// 浅拷贝 显示混入
function mixin(sourceObj, targetObj) {
for(var key in sourceObj) {
if(!targetObj.hasOwnProperty(key)) {
targetObj[key] = sourceObj[key];
}
}
return targetObj;
}
var Vehicle = {
enginess: 1,
array:["da",12],
ignition: function() {
console.log("Turing on my enginee");
},
drive: function() {
this.ignition();
console.log("Steering and moving forward");
}
};
var Car = mixin(Vehicle, {
wheels: 4,
ignition: function() {
console.log("ada");
},
drive: function() {
Vehicle.drive.call(this);
console.log("Rolling on all"+ this.wheels + "wheels");
}
})
console.log(Car);
Car.drive();
console.log(Car.enginess);
Car.array[0] = "ssss";
console.log(Car.array);
console.log(Vehicle.array);
// 隐式混入
// a = [1,2]
var Something = {
cool:function() {
// this.array = a;
this.array = [1,2];
this.greeting = "Hellow world";
this.count = 1;
}
}
Something.cool();
console.log(Something.greeting);
console.log(Something.count);
const Another = {
cool: function() {
Something.cool.call(this);
}
}
Another.cool();
console.log(Another.greeting);
Another.array[0] = 555;
console.log(Another.array);
console.log(Something.array);
console