//原型式继承
function inheritObject(o) {
//声明一个过渡函数对象
function F() {
//过渡对象的原型继承父对象
F.prototype = o;
}
//返回过渡对象的一个实例,该实例的原型继承了夫对象
return new F();
}
//测试
let book = {
name: 'js books',
alikeBook: ['css book', 'html book']
};
let newBook = inheritObject(book);
newBook.name = 'ajax book';
// newBook.alikeBook.push('xml book');
console.log(newBook.name); //ajax book
console.log(newBook.alikeBook); //undefined
console.log(book.name); //js books
console.log(book.alikeBook); //['css book', 'html book']
想问一下,问什么newBook.alikeBook
是undefined
?
慕的地10843
相关分类