javascript中的原型与继承2--构造函数窃取Constructor Stealing
2017-11-27 11:06
281 查看
Constructor Stealing,又叫object masquerading(对象伪装)或者classical inheritance。这种方法可以解决原型链中的问题。
Professional JavaScript for Web Developers中的原话是:
The basic idea is quite simple:call the supertype constructor form within the subtype constructor.Keeping in mind that functions are simply
objects that excute code in a particular context,the apply() and call() methods can be used to execute a constructor on the newly created object。
下面是例子:
//例子来源于Professional JavaScript for Web Developers,third edition ,Volume 1的207页
function SuperType() {
this.colors=['red','blue','green'];
}
function SubType() {
//inherit from SuperType
SuperType.call(this);
}
var instance1=new SubType();
instance1.colors.push('black');
console.log(instance1.colors);//[ 'red', 'blue', 'green', 'black' ]
var instance2=new SubType();
console.log(instance2.colors);//[ 'red', 'blue', 'green']
the SuperType constructor is called in the context of the newly created instance of SubType.
不管是使用apply还是call都可以传递参数,就不说了。
但是这种方式实现的继承也有问题,看下面
SuperType.prototype.shuohua=function () {
console.log('说话');
}
instance2.shuohua();//TypeError: instance2.shuohua is not a function
什么问题呢,就是 methods must be defined inside the constructor,so there is no function reuse.Furthermore,methods defined on the supertype's prototype
are not accessible on the subtype,so all types can use only the constructor pattern.
参考文献:
Professional JavaScript for Web Developers
Professional JavaScript for Web Developers中的原话是:
The basic idea is quite simple:call the supertype constructor form within the subtype constructor.Keeping in mind that functions are simply
objects that excute code in a particular context,the apply() and call() methods can be used to execute a constructor on the newly created object。
下面是例子:
//例子来源于Professional JavaScript for Web Developers,third edition ,Volume 1的207页
function SuperType() {
this.colors=['red','blue','green'];
}
function SubType() {
//inherit from SuperType
SuperType.call(this);
}
var instance1=new SubType();
instance1.colors.push('black');
console.log(instance1.colors);//[ 'red', 'blue', 'green', 'black' ]
var instance2=new SubType();
console.log(instance2.colors);//[ 'red', 'blue', 'green']
the SuperType constructor is called in the context of the newly created instance of SubType.
不管是使用apply还是call都可以传递参数,就不说了。
但是这种方式实现的继承也有问题,看下面
SuperType.prototype.shuohua=function () {
console.log('说话');
}
instance2.shuohua();//TypeError: instance2.shuohua is not a function
什么问题呢,就是 methods must be defined inside the constructor,so there is no function reuse.Furthermore,methods defined on the supertype's prototype
are not accessible on the subtype,so all types can use only the constructor pattern.
参考文献:
Professional JavaScript for Web Developers
相关文章推荐
- JavaScript 中实现继承的方式(列举3种在前一章,我们曾经讲解过创建类的最好方式是用构造函数定义属性,用原型定义方法。)
- javascript——混合继承(借用构造函数+原型继承)
- JavaScript继承基础讲解(原型链、借用构造函数、混合模式、原型式继承、寄生式继承、寄生组合式继承)
- javascript继承之借用构造函数与原型
- JavaScript面向对象(3)——原型与基于构造函数的继承模式(原型链)
- JavaScript继承基础讲解(原型链、借用构造函数、混合模式、原型式继承、寄生式继承、寄生组合式继承)
- javascript继承,原型链继承、借用构造函数、组合继承
- JS学习笔记——JavaScript继承的6种方法(原型链、借用构造函数、组合、原型式、寄生式、寄生组合式)
- javascript继承,原型继承,借用构造函数继承,混合继承
- JavaScript继承基础讲解(原型链、借用构造函数、混合模式、原型式继承、寄生式继承、寄生组合式继承)
- JavaScript构造函数+原型创建对象,原型链+借用构造函数模式继承父类练习
- 算法-javascript自实现继承extend(单次继承原型对象和单次继承构造函数)
- javascript继承之借用构造函数与原型
- javascript的构造函数,原型和以其实现的经典继承
- JavaScript继承基础讲解,原型链、借用构造函数、混合模式、原型式继承、寄生式继承、寄生组合式继承
- JavaScript 原型继承之构造函数继承
- JavaScript 原型继承之构造函数继承(转)
- Javascript面向对象编程(二):构造函数的继承
- JavaScript原型继承的小例子