javascript - How to set a lot a class properties from an object with the same property names? -


i'm working in js , i've got following object lot of properties:

var foo = {     prop1: 123,     prop2: 456,     prop3: 789,     ...     propx: 321 } 

i want set class same properties.

i can that:

this.prop1 = foo.prop1 this.prop2 = foo.prop2 this.prop2 = foo.prop2 ... this.propx = foo.propx 

but i'm looking like:

for(var property in foo) {     this.[property] = foo[property] } 

do know if it's possible kind of behavior in js?

i'd set class properties single loop for.

check if this has property , assign foo

for(var property in foo) {     if(this.hasownproperty(property)) {           this[property] = foo[property];     } } 

or better way loop keys in this , not need if statement.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -