Javascript 将对象作为参数传递给构造函数并将其属性复制到新对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4743030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing object as parameter to constructor function and copy its properties to the new object?
提问by VerizonW
I have a JavaScript constructor like this:
我有一个这样的 JavaScript 构造函数:
function Box(obj) {
this.obj = obj;
}
which i want to pass an object as a parameter like this:
我想将一个对象作为这样的参数传递:
var box = new Box({prop1: "a", prop2: "b", prop3: "c"})
and gives me something like this:
并给我这样的东西:
box.obj.prop1
box.obj.prop2
box.obj.prop3
but I would like the properties to be directly on the object like this:
但我希望属性直接在对象上,如下所示:
box.prop1
box.prop2
box.prop3
I know I could do something like this:
我知道我可以做这样的事情:
function Box(obj) {
this.prop1 = obj.prop1;
this.prop2 = obj.prop2;
this.prop3 = obj.prop3;
}
But that is not good because then my constructor would have to "know" before the names of the properties of the object parameter. What I would like is to be able to pass different objects as parameters and assign their properties directly as properties of the new custom object created by the constructor so I get box.propX
and not box.obj.propX
. Hope I am making myself clear, maybe I am measing something very obvious but I am a newbie so please need your help!
但这并不好,因为这样我的构造函数就必须在对象参数的属性名称之前“知道”。我想要的是能够将不同的对象作为参数传递,并将它们的属性直接分配为构造函数创建的新自定义对象的属性,因此我得到box.propX
而不是box.obj.propX
. 希望我说清楚了,也许我的意思很明显,但我是一个新手,所以请需要你的帮助!
Thanks in advance.
提前致谢。
采纳答案by Hemlock
You could do this. There is probably also a jquery way...
你可以这样做。可能还有一种jquery方式......
function Box(obj) {
for (var fld in obj) {
this[fld] = obj[fld];
}
}
You can include a test for hasOwnProperty if you've (I think foolishly) extended object
如果您(我认为是愚蠢的)扩展了对象,则可以包含对 hasOwnProperty 的测试
function Box(obj) {
for (var fld in obj) {
if (obj.hasOwnProperty(fld)) {
this[fld] = obj[fld];
}
}
}
Edit
编辑
Ah, ha! it's jQuery.extend
啊哈!它是jQuery.extend
So, the jQuery way is:
所以,jQuery 的方式是:
function Box(obj) {
$.extend(this, obj);
}
回答by glebm
Simply put this in your constructor
简单地把它放在你的构造函数中
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
this[prop] = obj[prop];
}
}
回答by Michael J. Calkins
Here's an example with the javascript module pattern:
这是 javascript 模块模式的示例:
var s,
NewsWidget = {
settings: {
numArticles: 5,
articleList: $("#article-list"),
moreButton: $("#more-button")
},
init: function(options) {
this.settings = $.extend(this.settings, options);
s = this.settings;
this.bindUIActions();
},
bindUIActions: function() {
s.moreButton.on("click", function() {
NewsWidget.getMoreArticles(s.numArticles);
});
},
getMoreArticles: function(numToGet) {
// $.ajax or something
// using numToGet as param
}
};
$(function(){
NewsWidget.init({
numArticles: 6
});
console.log(s.numArticles);
});