一.对象:普通对象 函数对象
二.构造函数特点: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===Objectobj.__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
js原型和原型链理解 ?constructor ?构造函数
原文地址:https://www.cnblogs.com/bais/p/9068928.html