原型链
如何构成原型链
原型链上属性的增删改查
绝大多数对象的最终继承自Object.prototype
Object.create(原型);
Grand.prototype.lastName = "liu";
function Grand (){
}
var grand = new Grand();
Father.prototype = grand;
function Father (){
this.name = "li";
}
var father = new Father();
Son.prototype = father;
function Son (){
this.hobbit = "ming";
}
var son =new Son();
查:查看属性就是可近的来,近的没有就往远的找,一直找到原型链的终端,终端要是没有,那就是undefined。
删:通过他子孙不能删,只能通过他本人删除。
改:通过他子孙不能改,只能通过他本人改。
增:通过他子孙不能增,只能通过他本人增。
Grand.prototype.lastName = "liu";
function Grand (){
}
var grand = new Grand();
Father.prototype = grand;
function Father (){
this.name = "li";
this.fortune = {
card1 : "visa"
}
}
var father = new Father();
Son.prototype = father;
function Son (){
this.hobbit = "ming";
}
var son =new Son();
这种改算不算改呢?这种改是你调用了这个东西,你在这个东西的基础上又给他增加了东西,相当于你调用了这个东西的方法,这个东西是个引用值,你给这个引用值加个东西,不相当于给fortune加个东西吗。这种意义的修改是引用值的修改。
Grand.prototype.lastName = "liu";
function Grand (){
}
var grand = new Grand();
Father.prototype = grand;
function Father (){
this.name = "li";
this.fortune = {
card1 : "visa"
};
this.num = 100;
}
var father = new Father();
Son.prototype = father;
function Son (){
this.hobbit = "ming";
}
var son =new Son();
son.num取的他爹的,取过来再赋值,就是自己的了
Person.prototype = {
name : "a",
sayName : function (){
console.log(this.name);
}
}
function Person (){
}
var person = new Person();
Person.prototype = {
name : "a",
sayName : function (){
console.log(this.name);
}
}
function Person (){
this.name = "b";
}
var person = new Person();
a.sayName() sayName里面的this指向是,谁调用的这个方法,this就是谁
Person.prototype = {
height : 100
}
function Person (){
this.eat = function (){
this.height ++;
//return undefined;
}
}
var person = new Person();
var obj = {};//他有原型吗?必须有原型
var obj1 = new Object();//,他俩是一样的