将对象作为参数传递给构造函数并将其属性复制到新对象?

时间:2022-12-05 14:32:21

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.

提前谢谢。

3 个解决方案

#1


20  

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);
}

#2


7  

Simply put this in your constructor

只需将其放入构造函数中

  for (var prop in obj) {
    if (obj.hasOwnProperty(prop)) {
      this[prop] = obj[prop];
    }
  }

#3


2  

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);
});

#1


20  

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);
}

#2


7  

Simply put this in your constructor

只需将其放入构造函数中

  for (var prop in obj) {
    if (obj.hasOwnProperty(prop)) {
      this[prop] = obj[prop];
    }
  }

#3


2  

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);
});