<分区>
我有一个对象构造函数,我定义了一个方法来使用 json 对象作为输入参数为对象属性设置新值,我的解决方案有效,但我怀疑是有最好的方法吗?
任何使用OOP 的答案将不胜感激。
这是我的代码:
//Object constructor
function searchOption(o) {
o = o || {};
this.Word = o.Word || "";
this.SubCategories = o.SubCategories || [];
this.Page = o.Page || 1;
this.Sort = o.Sort || "desc";
this.ItemsView = o.ItemsView || 18;
//Method to set new values, preserving untouched properties
this.set = function (x) { $.extend(this, x); };
}
(function() {
var search = new searchOption({ Word:"canela", ItemsView:6 });
console.log(search);
search.set({ Word:"VIAJE", Page:3, Sort:"asc" });
console.log(search);
})();