js的原型链

原型链:

        1.如何构成原型链?

        2.原型链上属性的增删改查。

        3.绝大多数对象的最终都会继承自Object.prototype    (var obj = Object.create(null或者undefined)没有原型)。

        4.Object.create(原型)。

构成原型链和操作原型链属性:
//最顶的原型是Object.prototype
Grand.prototype.__proto__ = Object.prototype;
Grand.prototype.lastName = "Deng";
function Grand(){
  
};

var grand = new Grand();

Father.prototype = grand;
function Father(){
    this.name = "xuming";
    this.fortune = {
        card1 : "visa",
    };
    this.num = 100;
};

var father = new Father();

Son.prototype = father;
function Son(){
    this.hobbit = "smoke";
};

var son = new Son();

//操作原型链上的属性,son会在自身生成一个属性
son.num++; // ---> son.num = son.num + 1;
console.log(father.num); //100
console.log(son.num); //101

//操作原型链上的对象中的属性,son不会生成属性
son.fortune.card2 = 'master'
console.log(son.fortune); //{card1 : 'visa',card2 : 'master'}
console.log(father.fortune); //{card1 : 'visa',card2 : 'master'}
原型中this的指向:
//a.sayName() sayName里面的this指向是,谁调用的这个方法,this就是指向谁
Person.prototype = {
    name : "a",
    sayName : function(){
        console.log(this.name);
    }
    }

function Person(){
    this.name = "b";
}
        
var person = new Person();
person.sayName(); //b
person.__proto__.sayName(); //a
Object.create(原型):

对象形式:

var obj = {name : "sunny"};
var obj1 = Object.create(obj);

console.log(obj1.name); //sunny

函数形式:

Person.prototype.name = "sunny";
function Person(){

}
var person = Object.create(Person.prototype);
var person1 = new Person();

console.log(person.__proto__.name); //sunny
console.log(person1.__proto__.name); //sunny

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值