javascript的Mixin继承模式失去了Closure特性
Mixin inheritance pattern of javascript lose Closure feature
总之,我知道定义javascript OOP对象的传统方法。以下是示例。
var TField=function(jData)
{
this.id=jData.id;
this.name=jData.name;
this.attributes=jData.attributes;
TField.prototype.render=function(){
alert(jData.id);
};
};
我们可以发现TField.prototype.render
知道jData
的值,这是由于Closure
的特性。
但现在我只是试图在javascript中实现继承,我发现推荐的方式是Mixin pattern
。下面是我到目前为止所做的代码:
TChildField.prototype.render=function(){
alert('TChildField render.');
//Can not utilize the parameters of the constructor. like jData
};
var TField=function(jData)
{
this.id=jData.id;
this.name=jData.name;
this.attributes=jData.attributes;
};
TField.prototype.render=function(){
alert('TField render.');
};
var TChildField=function(jData)
{
TField.call(this,jData)
}
var tobj={id:"1",name:"test",attribute:{}};
TChildField.prototype=Object.create(TField.prototype)
TChildField.prototype.render=function(){
alert('TChildField render.');
};
var c= new TChildField(tobj);
alert(c.id);
alert(c.name);
很好,没有问题。但我的问题是我发现我不能像TField一样利用TChildField
的构造函数的参数jData
。我知道我可以定义this.Data=jData
,这样我就可以在TChildField.prototype.render
中使用它,但我不想把它作为一个属性。最糟糕的是失去了Closure
的好处,有办法做到吗?还是我不知道的?谢谢。
需要这种封装做什么?设置this.Data=jData
有什么问题?这很简单,而且能解决你的问题。为什么最糟糕的事情是失去关闭的好处?实际上应该避免闭包,因为它们可能会导致非常可怕的问题(内存泄漏?)。
相关文章:
- 使用lodash的特定对象特性值
- Google Closure Advanced |无法识别对象属性|动态属性
- 铁路超高接入特性“;createdAt”;云代码解析框架
- ECMAScript 6的哪些特性意味着严格模式
- Angularjs$watch内部使用Closure
- 如何在高级编译模式下使用 Google Closure 编译器编译 jQuery UI 小部件
- 如何按一个仍保持字母顺序的特性值(另一个特性值)对关联数组进行排序
- RegExp的Google Closure编译器警告
- 应用构造函数特性没有结果
- QML:如何等到由于特性更改而重新绘制零部件
- Javascript closure?
- 有没有一种方法可以创建一个对象并同时为其指定一个动态特性
- 需要绕过ExtJS的异步特性
- 聚合物:如何为具有嵌套特性的图纸选项卡设置默认值
- 具有两种不同特性的车把.helper或partial
- Google Closure编译器为命名空间创建了不完整的别名
- 对Angular中的两个特性使用一个函数
- 如何在创建后立即显示关联模型的特性
- 在实现Closure特性(javascript)时陷入困境
- javascript的Mixin继承模式失去了Closure特性