Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing object as parameter to constructor function and copy its properties to the new object?

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.

like image 597
VerizonW Avatar asked Jan 20 '11 02:01

VerizonW


People also ask

When you pass an object as a parameter does the original object get changed?

2.2. When a parameter is pass-by-reference, the caller and the callee operate on the same object. It means that when a variable is pass-by-reference, the unique identifier of the object is sent to the method. Any changes to the parameter's instance members will result in that change being made to the original value.

What are the properties of an object created with a new operator and the object constructor?

The new operator is used to create an instance of an object. To create an object, the new operator is followed by the constructor method. In the following example, the constructor methods are Object(), Array(), and Date(). These constructors are built-in JavaScript functions.

When an object is instantiated from a constructor function it copies the properties and methods from the prototype into the new object?

Working With Object PrototypesWhen an object is instantiated from a constructor function, it copies the properties and methods from the prototype into the new object. However, this can result in inefficient use of memory and resources.

Which JavaScript property allows you to add new properties to object constructor?

Assigning the constructor property to an object One can assign the constructor property of non-primitives. const arr = []; arr. constructor = String arr. constructor === String // true arr instanceof String // false arr instanceof Array // true const foo = new Foo(); foo.


2 Answers

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); } 
like image 127
Hemlock Avatar answered Sep 24 '22 11:09

Hemlock


Simply put this in your constructor

  for (var prop in obj) {     if (obj.hasOwnProperty(prop)) {       this[prop] = obj[prop];     }   } 
like image 23
glebm Avatar answered Sep 26 '22 11:09

glebm