繼承方式一:混入繼承
"use strict";
//object:A
let Oa= {
money: 'many money',
resource: 'company,house',
control: function (){
console.log('this is function');
}
};
//object:B
let Ob = {};
for (let k in Oa) {
Ob['k'] = Oa['k'];
}
console.log(Ob.money);//'many money'
console.log(Ob.control()); //'this is function'
繼承方式二:原型繼承
//object:A
var Oa= {
loveColor: 'Green',
habbit: 'backetball',
sayHi: function (){
console.log('hello world');
}
}
//建立構造函數,而且將對象A的地址賦值給構造函數的原型
function Func() {}
//實現繼承
Func.prototype = Oa;
//恢復三角關係
Func.prototype.contructor = Func;
//實例化的全部對象都擁有對象Oa的屬性和方法
//object:B
let Ob = new Func();
console.log(Ob.lovaColor); //'Green';
console.log(Ob.habbit); //'backetball';
繼承方式三:混入加原型
//object:A
let Oa = {
color: 'red',
love: function (){
console.log('love green');
}
}
function Func(name){
this.name = name;
}
for (let k in Oa) {
Func.prototype[k] = Oa[k];
}
//object:B
let Ob = new Func('jeky');
console.log(Ob.color) // red;
Ob.love() // 'love green';
繼承方式四:object.create()
;
//object:A
let Oa = {
fName: 'apple',
color: 'red'
};
//object:B
//實現過程:new了一個新的對象,把傳入做爲參數的那個對象設置給新建立的這個對象的原型屬性
let Ob = Object.create(Oa);
console.log(Ob.fName); //'apple'