分享一下 我对原型和原型链的理解
原型对象:
function People(nameValue,ageValue,fondValue)
{
this.name = nameValue;
this.age = ageValue;
this.fond = fondValue;
}
People.prototype.rule = function()
{
console.log(this.name+'说:人人皆需奉献,人人都有一死')
}
People.prototype =
{
insteresting:'烤红薯',
work:function()
{
console.log('搬砖')
}
}
var p1 = new People('大傻',25,'哭')
var p2 = new People('二傻',22,'吃')
// 先从对象中找属性 找不到再从对象的原型中找该属性
console.log(p1.insteresting)
// 对象属性的修改影响不到原型的内容
p1.insteresting = '大西瓜';
console.log(p1.insteresting) //大西瓜
console.log(p2.insteresting) //烤红薯
People.prototype.insteresting = '大榴莲'
console.log(p2.insteresting) //大榴莲
---------------------------------------------------------------------------------------------------------------
原型链:
function Baby(name,age,birthDay)
{
this.name = name;
this.age = age;
this.birthDay = birthDay;
}
Baby.prototype.eat = function(food)
{
console.log(this.name + '吃了' + food)
}
Baby.prototype.sleep = function(time)
{
console.log(this.name +'睡了' + time + '个小时')
}
var baby = new Baby('葫芦娃',1,new Date(2017,1,1))
console.log(baby.name)
baby.eat('奶粉')
baby.sleep('18')
function Child(name,age,birthDay)
{