【问题标题】:jQuery Extend copying over previous closuresjQuery 在以前的闭包上扩展复制
【发布时间】:2016-03-29 13:24:44
【问题描述】:

说明

在我的示例中,我尝试使用private 成员和public getter 定义一个简单的类(称为Car)。我还有另一个对象,我想将其映射到我的类的实例中。我正在使用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!

小提琴:

https://jsfiddle.net/sajjansarkar/samj3pjq/2/

[编辑]我的解决方案基于 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();

更新的小提琴:

https://jsfiddle.net/sajjansarkar/samj3pjq/3/

【问题讨论】:

    标签: javascript jquery closures


    【解决方案1】:

    你为什么要那样做?也许这会更简单:

    // 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();

    【讨论】:

    • 谢谢!虽然我不希望更改我的类的构造函数,但它确实让事情变得更容易。在我的情况下,我必须使函数通用并处理类的任何实例,所以我不能直接使用 new Car() 构造函数,但我已经使用了你的建议,谢谢!
    猜你喜欢
    • 2011-11-13
    • 2013-12-12
    • 2014-09-16
    • 1970-01-01
    • 2016-08-02
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多