jQuery扩展对以前闭包的复制
jQuery Extend copying over previous closures
描述
在我的示例中,我试图定义一个简单的类(称为Car
),该类具有private
成员和public
getter。我还有另一个对象,我想将它映射到我的类的实例中。我使用jQuery.extend()
来实现这一点,它运行良好,除非成员是私有的(与此无关)。我猜这是因为getter函数已经关闭了在对象创建时定义的值。
我仍然希望能够在.extend()
之后重新关闭成员的函数,这样它将反映新的值,有人能帮助我实现这一点吗?
我还尝试将传入的引用扩展到{}对象,然后使用该空白对象作为目标,如下所示:
function mapToObject(template, json) {
var b = $.extend(true,{}, template);
return $.extend(true, b, json);
}
这也没有打破之前的关闭。
我的代码:
// simple class with private member
function Car() {
var noOfWheels = 2;
this.honk = function() {
alert(noOfWheels);
}
}
// method which converts JSON object to a more typed class object
function mapToObject(template, json) {
return $.extend(true, template, json);
}
// sample Object , in reality coming from AJAX response
var t = {
noOfWheels: 5
};
// i want to map t to an instance of Car
var r = mapToObject(new Car, t);// closing here during new Car, i think
r.honk(); // this alerts 2 not 5 because of closure!
Fiddle:
https://jsfiddle.net/sajjansarkar/samj3pjq/2/
[EDIT]我的解决方案基于DrFlink的回答:
// one class
function Car(options) {
var settings = $.extend({
noOfWheels: 2
}, options || {});
this.honk = function() {
alert(settings.noOfWheels);
}
}
// another class
function Lemon(options) {
var settings = $.extend({
color: null
}, options || {});
this.getColor = function() {
alert(settings.color);
}
}
// function to map any json to a template class instance
function ObjectMapperFactory(template, json) {
if (!jQuery.isArray(json)) {
return new template(json);
} else {
var returnArray = [];
$(json).each(function() {
returnArray.push(new template(this))
});
return returnArray;
}
}
//test 1
var carJSON = {
noOfWheels: 5
};
var c = ObjectMapperFactory(Car, carJSON);
c.honk();
//test2 -different class and also testing array
var lemonJSON = [{
color: "yeller"
},{
color: "red"
}];
var c = ObjectMapperFactory(Lemon, lemonJSON);
c[0].getColor();
c[1].getColor();
更新的FIDDLE:
https://jsfiddle.net/sajjansarkar/samj3pjq/3/
为什么要这样做?也许这会更简单:
// simple class with private member
var Car = function(options) {
var settings = $.extend({
noOfWheels: 2
}, options || {});
// Public method - can be called from client code
this.honk = function(){
alert(settings.noOfWheels);
};
// Private method - can only be called from within this object
var myPrivateMethod = function(){
alert('This is private!');
};
};
// sample Object
var t = {
noOfWheels: 4
};
var I = new Car(t);
var J = new Car({
noOfWheels: 6
});
I.honk();
J.honk();
相关文章:
- 在underscorejs模板中使用闭包
- setTimeout可以与闭包内的函数一起使用吗
- 附加到原型属性的Do函数没有闭包
- 使用闭包共享构造函数参数
- 使用Google闭包编译器包含一个Ecmascript 6类
- 从js引擎的角度来看闭包和构造函数是如何工作的
- for循环中的JavaScript闭包
- Javascript闭包-如何防止内存泄漏
- 子类访问父类's闭包变量
- 闭包如何具体化数据封装
- Javascript.闭包和dynamic'这'实际上具有约束力
- 构造函数函数闭包变量
- 闭包js框架-将ArrayBuffer转换为字符串
- 如何在Angularjs中重构闭包中的重复代码
- 如何告诉闭包javascript编译器不要混淆webkitAudioContext的方法名称
- Google闭包和生成的getters/ssetter
- 如何冻结函数's在闭包中的变量
- jQuery扩展对以前闭包的复制
- 如何复制(新实例)闭包函数
- 防止闭包编译器复制字符串