原型式继承

2019-07-14  本文已影响0人  南蓝NL
function object(o){
 function F(){}
 F.prototype = o;
 return new F();
}
var dog = {
  species: '比熊犬',
  color: 'gold',
  skills: ["jump","climb","catch","run"]
}

var dog1 = Object(dog); //  dog 对象作为dog1 对象的基础,在ES5当中,这里可以写成Object.create
dog1.species = ' 泰迪';
dog1.color = 'brown';
dog1.skills.push('acting');

var dog2 = Object(dog);
dog2.species = ' 吉娃娃';
dog2.color = 'grey';
dog2.skills.push('show');
skills: ["jump","climb","catch","run"];
 
console.log(dog.skills); // ["jump", "climb", "catch", "run", "acting", "show"]
console.log(dog.species); // 吉娃娃
上一篇下一篇

猜你喜欢

热点阅读