-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
寄生式继承的基本思路是什么?有什么优缺点? #37
Comments
寄生式继承就是把原型式继承再次封装,然后在对象上扩展新的方法,再把新对象返回
|
寄生式继承:创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后返回对象。 |
寄生式继承
寄生式继承是在原生式继承的基础上,给对象增加一些额外的属性,方法等。 |
创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后像真的是它做了所有工作一样,返回对象。
缺点:使用寄生式继承来为对象添加函数,会由于不能做到函数复用而降低效率。 |
寄生式继承是与原型式继承紧密相关的一种思路。寄生式继承的思路与寄生构造函数和工厂模式类似,即创建一个仅用于封装继承过程的函数,该函数在内部已某种方式来增强对象,最后再像真地是它做了所有工作一样返回对象。 function createAnother(original) {
var clone = object(original);//通过调用函数创建一个新对象
clone.sayHi = function () {//以某种方式增强这个对象
console.log('hi');
};
return clone;//返回这个对象
}
var person = {
name: 'Yvette',
hobbies: ['reading', 'photography']
};
var person2 = createAnother(person);
person2.sayHi(); //hi 基于
|
原型式继承 是将传入的对象作为要返回对象的原型。 实现如下:
|
function createDog(obj){
var clone = Object(obj);
clone.getColor = function(){
console.log(clone.color)
}
return clone
}
var dog = {
species: '贵宾犬',
color: 'yellow'
}
var dog1 = createDog(dog);
dog1.getColor(); // yellow |
No description provided.
The text was updated successfully, but these errors were encountered: