zoukankan      html  css  js  c++  java
  • js创建对象之原型模式3、更简洁的创建原型模式

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title></title>
    	</head>
    	<body>
    		<script type="text/javascript">
    		//创建对象
    		//原型模式
    		//更简单的原型语法
    		//之前为原型添加属性都要写一遍prototype,麻烦,解决方法如下
    		function Person(){};
    		Person.prototype = {
    			name: "宝清老窖",
    			age: 30,
    			job: "Software",
    			sayName: function(){
    				console.log(this.name);
    			}
    		}
    		//简便了不少哈,但是constructor属性不再指向Person了,因为我们重写了prototype
    		//虽然instanceof还能返回正确的结果,但是constrctor已经无法确定对象的类型
    		var friend = new Person();
    		console.log(friend instanceof Object);	//true
    		console.log(friend instanceof Person);	//true
    		
    		console.log(friend.constructor == Person);	//false
    		console.log(friend.constructor == Object);	//true
    		
    		console.log('---------------------------------------------------');
    		//所以需要改造成下面的方式
    		function Person(){};
    		Person.prototype = {
    			constructor: Person,
    			name: "宝清老窖",
    			age: 30,
    			job: "Software",
    			sayName: function(){
    				console.log(this.name);
    			}
    		}
    		var friend = new Person();
    		console.log(friend instanceof Object);	//true
    		console.log(friend instanceof Person);	//true
    		
    		console.log(friend.constructor == Person);	//true
    		console.log(friend.constructor == Object);	//false
    		//但是以这种方式的话,constructor的[[Enumerable]]为true
    		//所以得改成false
    		function Person(){};
    		Person.prototype = {
    			name: "宝清老窖",
    			age: 30,
    			job: "Software",
    			sayName: function(){
    				console.log(this.name);
    			}
    		}
    		Object.defineProperty(Person.prototype , 'constructor' , {
    			enumerable: false,
    			value: Person
    		})
    		//Object.keys(Person.prototype);为		["name", "age", "job", "sayName"]
    		</script>
    	</body>
    </html>
    

      提取出js

    //创建对象
    		//原型模式
    		//更简单的原型语法
    		//之前为原型添加属性都要写一遍prototype,麻烦,解决方法如下
    		function Person(){};
    		Person.prototype = {
    			name: "宝清老窖",
    			age: 30,
    			job: "Software",
    			sayName: function(){
    				console.log(this.name);
    			}
    		}
    		//简便了不少哈,但是constructor属性不再指向Person了,因为我们重写了prototype
    		//虽然instanceof还能返回正确的结果,但是constrctor已经无法确定对象的类型
    		var friend = new Person();
    		console.log(friend instanceof Object);	//true
    		console.log(friend instanceof Person);	//true
    		
    		console.log(friend.constructor == Person);	//false
    		console.log(friend.constructor == Object);	//true
    		
    		console.log('---------------------------------------------------');
    		//所以需要改造成下面的方式
    		function Person(){};
    		Person.prototype = {
    			constructor: Person,
    			name: "宝清老窖",
    			age: 30,
    			job: "Software",
    			sayName: function(){
    				console.log(this.name);
    			}
    		}
    		var friend = new Person();
    		console.log(friend instanceof Object);	//true
    		console.log(friend instanceof Person);	//true
    		
    		console.log(friend.constructor == Person);	//true
    		console.log(friend.constructor == Object);	//false
    		//但是以这种方式的话,constructor的[[Enumerable]]为true
    		//所以得改成false
    		function Person(){};
    		Person.prototype = {
    			name: "宝清老窖",
    			age: 30,
    			job: "Software",
    			sayName: function(){
    				console.log(this.name);
    			}
    		}
    		Object.defineProperty(Person.prototype , 'constructor' , {
    			enumerable: false,
    			value: Person
    		})
    		//Object.keys(Person.prototype);为		["name", "age", "job", "sayName"]
    

      

  • 相关阅读:
    httpModules与httpHandlers之httpModules(转载)
    一个人的命运决定于晚上8点到10点之间【认真看完本篇文章,你的人生将会有所改变】
    关于错误Access Violation和too many consecutive exceptions 解决方法
    Delphi PChar与String互转
    Delphi+MySQL:TADOQuery使用插入中文乱码解决方法
    Delphi中的操作二进制文件的两个重要函数
    JavaWeb错误处理集锦
    Codeforces Round #286 (Div. 1) B. Mr. Kitayuta&#39;s Technology (强连通分量)
    从头认识java-16.4 nio的读与写(ByteBuffer的使用)
    php在数字前面补0得到固定长度数字的两种方法
  • 原文地址:https://www.cnblogs.com/xudy/p/5427049.html
Copyright © 2011-2022 走看看