js创建对象之原型模式2原型与in操作符

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
		//创建对象
		//原型模式
		//2、原型与in操作符
		//有两种方式使用in操作符:1、for-in;2、单独使用时,对象能返回给定属性的时候后,返回true,即:无论属性在实例还是原型中。
		function Person(){};
		Person.prototype.name = "宝清老窖";
		Person.prototype.age = 29;
		Person.prototype.job = "Soft Ware";
		Person.prototype.sayName = function(){
			console.log(this.name);
		}
		
		var person1 = new Person();
		var person2 = new Person();
		console.log('hasOwnPropertye: ' + person1.hasOwnProperty('name'));	//false
		console.log('in: ' + ('name' in  person1));			//true
		
		person1.name = "大美女";
		console.log('hasOwnPropertye: ' + person1.hasOwnProperty('name'));	//true
		console.log('in: ' + ('name' in person1));			//true
		delete person1.name;
		//可以看出以上代码,只要原型或者实例上有属性,那么in就返回true
		//所以同时使用hasOwnPropertye和in 就可以判断出咱们访问到的属性是在实例上还是原型上
		function hasPrototypeProperty(object , name){
			return !object.hasOwnProperty(name) && (name in object);
		}
		
		console.log(hasPrototypeProperty(person1 , 'name'));	//true
		person1.name = "大美女";
		console.log(hasPrototypeProperty(person1  , 'name'));	//true
		
		//在使用for-in循环时,返回的实所有能通过对象访问的、可枚举的(enumerated)属性
		//包括存在于实例中的属性,也包括存在于原型中的属性。
		var o = {
			toString: function(){
				return "My Object";
			}
		}
		
		for(var prop in o){
			if(prop == "toString"){
				alert("Found toString");
			}
		}
		//Object.keys取得对象上所有可以枚举上的实例属性
		var keys = Object.keys(Person.prototype);
		console.log(keys);	//["name","age","job","sayName"]
		
		var p1Keys = Object.keys(person1);
		console.log(p1Keys);	//['name']
		
		//如果想要得到所有实例属性,不管是否枚举的话Object.getOwnPropertyNames();
		var pNames = Object.getOwnPropertyNames(Person.prototype);
		console.log(pNames);	//["constructor", "name", "age", "job", "sayName"]
		//注意,这里包含了不可枚举的constructor
		
		
		</script>
	</body>
</html>

  

  提取js

//创建对象
		//原型模式
		//2、原型与in操作符
		//有两种方式使用in操作符:1、for-in;2、单独使用时,对象能返回给定属性的时候后,返回true,即:无论属性在实例还是原型中。
		function Person(){};
		Person.prototype.name = "宝清老窖";
		Person.prototype.age = 29;
		Person.prototype.job = "Soft Ware";
		Person.prototype.sayName = function(){
			console.log(this.name);
		}
		
		var person1 = new Person();
		var person2 = new Person();
		console.log('hasOwnPropertye: ' + person1.hasOwnProperty('name'));	//false
		console.log('in: ' + ('name' in  person1));			//true
		
		person1.name = "大美女";
		console.log('hasOwnPropertye: ' + person1.hasOwnProperty('name'));	//true
		console.log('in: ' + ('name' in person1));			//true
		delete person1.name;
		//可以看出以上代码,只要原型或者实例上有属性,那么in就返回true
		//所以同时使用hasOwnPropertye和in 就可以判断出咱们访问到的属性是在实例上还是原型上
		function hasPrototypeProperty(object , name){
			return !object.hasOwnProperty(name) && (name in object);
		}
		
		console.log(hasPrototypeProperty(person1 , 'name'));	//true
		person1.name = "大美女";
		console.log(hasPrototypeProperty(person1  , 'name'));	//true
		
		//在使用for-in循环时,返回的实所有能通过对象访问的、可枚举的(enumerated)属性
		//包括存在于实例中的属性,也包括存在于原型中的属性。
		var o = {
			toString: function(){
				return "My Object";
			}
		}
		
		for(var prop in o){
			if(prop == "toString"){
				alert("Found toString");
			}
		}
		//Object.keys取得对象上所有可以枚举上的实例属性
		var keys = Object.keys(Person.prototype);
		console.log(keys);	//["name","age","job","sayName"]
		
		var p1Keys = Object.keys(person1);
		console.log(p1Keys);	//['name']
		
		//如果想要得到所有实例属性,不管是否枚举的话Object.getOwnPropertyNames();
		var pNames = Object.getOwnPropertyNames(Person.prototype);
		console.log(pNames);	//["constructor", "name", "age", "job", "sayName"]
		//注意,这里包含了不可枚举的constructor
		

  

原文地址:https://www.cnblogs.com/xudy/p/5424220.html