这就是我从属性哈希创建对象的方式:
var object = new function (data) {
var self = this;
self.property = data.property;
self.anotherProperty = data.anotherProperty;
self.method = function () { return 'something'; }
self.update = function (newData) {
//what is here ?
//i could have written:
self.property = newData.property;
self.anotherProperty = newData.anotherProperty;
//but why not reuse the constructor?
}
};
我想知道如何重用这个函数(构造函数)来从哈希更新对象。以便:
object.update(newData)
newData
将像在构造函数中完成的那样从哈希更新当前对象属性。