一.對象:普通對象 函數對象
二.構造函數特點:1.需要new實例化,內部使用this對象指向即將要生成的實例對象 2.首字母大寫,用於區分普通函數
function Person(name){ this.name=name } var person1=new Person('xiaohong') var person2=new Person('lili')
person1.constructor=Person constructor指向構造函數,Person的內置屬性 Person.prototype(函數對象),每個原型對象都有一個constructor屬性,指向prototype屬性所在的函數Person
即person1.constructor=person2.constructor=Person.prototype.constructor
通過原型實現繼承
三.__proto__,每個對象都有這個屬性指向創建他的構造函數的原型即person1.__proto__=Person.prototype
//var obj={} var obj=new Object(); obj.constructor===Object obj.__proto__===Object.prototype
person1.__proto__是Person.prototype
Person.__proto__是Person是構造函數,Function,即Function.prototype
Person.prototype._proto__是 Person.prototype是原型對象,Object,即Object.prototype
Object.__proto__是 Objext是構造行數Function 即Function.prototype
Object.prototype.__ptoto__是 Object.prototype的原型對象的__proto__指向構造函數的prototype,處於頂層是null
四。Math和Json是以對象存在的 即Math.__proto__===Object.prototype
五。Function.prototype是唯一一個typeof Function.prototype是function的prototype,其他構造器的prototype都是object
六。注意constructor的指向例如
function Animal(){ } Animal.prototype.age='20' function Cat(name,color){ this.name = name this.color = color this.name2 = '11112222' } Cat.prototype = new Animal(); Cat.prototype.type="貓" Cat.prototype.eat=function(){ console.log('愛吃魚') } function Dog(name){ this.name=name } Dog.prototype = new Animal(); Dog.prototype={ getName:function(){ console.log('gougou') } }
Cat.prototype.constructor === Animal Dog.prototype.constructor===Object 兩者不相同,后者是重寫Animal.prototype,前者是修改。
七。
- 原型和原型鏈是JS實現繼承的一種模型。
- 原型鏈的形成是真正是靠
__proto__而非prototype
