35

I have a JavaScript constructor like this:

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!

Thanks in advance.

1
  • maybe use prototype? Commented Jan 5, 2019 at 8:05

4 Answers 4

25

You could do this. There is probably also a jquery way...

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

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

Edit

Ah, ha! it's jQuery.extend

So, the jQuery way is:

function Box(obj) {
  $.extend(this, obj);
}
Sign up to request clarification or add additional context in comments.

2 Comments

One thing to note neither solution is doing a deep copy. Depending on your actual implementation there is potential for problems. For example if instead of passing your object anonymously you create a variable obj={ prop1:..., propDanger: { a:1, b:2 } } and pass that to your constructor var box = new Box(obj), then later edits to obj.propDanger.a will also change box.propDanger.a.
@Brett Good point. Rarely a problem, but a good think to know so you don't get surprised by it when it does happen.
9

Simply put this in your constructor

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

1 Comment

I am giving the accepted answer to @Hemlock because he got it first, but thanks really!
8

Really short

function Box(obj) {
  Object.assign(this, obj);
}

1 Comment

way better than doing this[prop] = obj[prop].
2

Here's an example with the javascript module pattern:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.