标签:
__proto__:所有对象都有的属性。
var obj = {}; console.log(obj.__proto__); //{} var obj1 = new Object(); console.log(obj1.__proto__); //{} var func = function() {}; console.log(func.__proto__); //[Function:Empty]
prototype:函数对象中的属性。
var obj = {}; var func = function() {}; console.log(func.prototype); //{} console.log(obj.prototype); //undefined
所有对象的root为object,object的__proto__为null
var obj = {}; console.log(obj.__proto__); //{} console.log(obj.__proto__.__proto__); //null var func = function() {}; console.log(func.__proto__); //[Function:Empty] console.log(func.__proto__.__proto__); //{} console.log(func.__proto__.__proto__.__proto__);//null
使用构造函数创建的对象中,__proto__为构造函数的prototype指向的对象
var Func = function() {}; Func.prototype.name = ‘f1‘; console.log(Func.prototype); //{name:‘f1‘} var obj = new Func(); console.log(obj.__proto__); //{name:‘f1‘} console.log(Func.prototype==obj.__proto__); //true
实现继承时,__proto__的指向:dog1 instance->Dog.prototype->Animal.prototype->object->null
function Animal() {} Animal.prototype.name = ‘animal‘; function Dog() {} Dog.prototype = new Animal(); Dog.prototype.height = 10;
console.log(Animal.prototype); //{name:‘animal‘}
console.log(Dog.prototype); //{height:10} var dog1 = new Dog(); console.log(dog1); //{} console.log(dog1.__proto__); //{height:10} console.log(dog1.__proto__.__proto__); //{name:‘animal‘} console.log(dog1.__proto__.__proto__.__proto__);//{} object console.log(dog1.__proto__.__proto__.__proto__.__proto__);//null root
console.log(dog1.name); //animal
console.log(dog1.height); //10
额,,刚学JavaScript,暂时就想到这么多。
标签:
原文地址:http://www.cnblogs.com/sttyzh/p/4619723.html