原型:JS中的对象都包含了一个prototype的内部属性,这个属性所对应的就是该对象的原型。
1.原型
1.1 prototype 和 proto
所有实例对象都有proto属性指向构造该对象的原型(构造函数的属性 prototype)。(看下列代码第十五行注释)
<script>//新建一个构造函数var name = "马化腾"function Person(name,age){this.name = name;this.age = age;}Person.prototype.sayName=function(){console.log(this.name);}var bb = new Person("马云",56);console.log(bb);console.log(bb.__proto__);console.log(Person.prototype);//Person.prototype==bb.__proto__bb.sayName();//首先先看构造函数里面有没有sayName方法,没有就去__proto__原型里面看有没有,没有就在进入下一层级__proto__原型看有没有</script>
关系图:
2.原型链
当访问一个对象的某个属性时,会先在这个对象本身属性上查找,如果没有找到,则会去它的__proto__隐式原型上查找,即它的构造函数的prototype,如果还没有找到就会再在构造函数的prototype的__proto__中查找,这样一层一层向上查找就会形成一个链式结构,我们称为原型链。
示例:
function Parent(month){this.month = month;}var child = new Parent('Ann');console.log(child.month); // Annconsole.log(child.father); // undefined
在child中查找某个属性时,会执行下面步骤:

