js __proto__ prototype constructor

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	
</body>
</html>

<script>
	function person(name){
		this.name  = name;
	}	

	person.prototype.getname = function(){
		return this.name;
	}

	var objectFactory = function(){
		var obj  = new Object();
		console.log(obj.__proto__);
		_construct = [].shift.call(arguments);

		//更改的是构造器的原型.  默认的是 function object / 更改后的指定的.	
		obj.__proto__ = _construct.prototype;
		var ret  = _construct.call(obj,arguments);

		return typeof ret  === 'object' ? ret  : obj;

	}

	var newObj  = objectFactory(person,"zhan");
	console.log(newObj.__proto__);
	//console.log(newObj.getname());
	// console.log(newObj);

	var a={name:'zhagn'};
	console.log(a);
	// 若是是这样的调整,那么a就变成一个函数对象,
	//a.__proto__  = person.__proto__;   // 这样赋值是错误的.
	// a.__proto__ = person.constructor;  // 这样赋值也是错误的,
	// 后改变原型对原始对象会有影响,不存在前后的问题.
	a.__proto__  = person.prototype;
	console.log(a);
	//console.log(a.getname());
	// new a;  //  a is not a constructor ,若是用new的方式, 必须有constructor属性,
	// a();    //  a is not a function, 也不能够调用, 
	// a.toString();  //Function.prototype.toString is not generic  
	//  a.name;   // 这个是能够正常使用的,
	/*
	*  关键的区别 :   __proto__ : 是一个函数,
	*                 construct : 是一个函数,
	*				  prototype : 是一个对象. 建立对象,主要依赖于此.	
	*				  字面量对象不能够 new 的关键缘由是  该对象不具备constructor属性.	
	**/
	// 经过__proto__原型构造对象.
	//console.log(new a.__proto__.constructor('aaa'));
	var ar = new Array();
	var ob = new Object();
	console.log(ar.__proto__);   // 该__proto__中包括不一样的方法.
	console.log(ob.__proto__);   // 

	console.log(ar.__proto__ != ob.__proto__,  ar.prototype == ob.prototype);

	console.log(a.prototype == newObj.prototype );
	console.log(a.__proto__ == newObj.__proto__);

</script>
相关文章
相关标签/搜索