oop - Javascript assigning object values to new object -


so using options pass values new object in literal object fashion.

var obj = new myobject({height:'500',width:'300');   function myobject(options){  } 

i'm not sure best route these values assigned object though work.

 function myobject(options){  ...assignment...     alert(this.width);      } 

function myobject(options){     // copy options current object    (var key in options) {        if (options.hasownproperty(key)) {            this[key] = options[key];        }    }     alert(this.width);     // 300 }  var obj = new myobject({height:'500',width:'300'}); 

you can extend concept can have default property values myobject, , can override them options object:

function myobject(options){     // defaults     this.name = 'box';     this.width = '100';     this.height = '100';     // copy options current object    (var key in options) {        if (options.hasownproperty(key)) {            this[key] = options[key];        }    }     alert(this.width);  }  var obj = new myobject({height:'500',width:'300'});     // alert: 300 var obj2 = new myobject({height: '500'});               // alert: 100 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

ios - iPhone/iPad different view orientations in different views , and apple approval process -

Shrink a YouTube video to responsive width -