Using a pre-defined method to change a property of an object Javascript -


i have function defined before object created. pre-defined function uses 'this' keyword change value of property in object. inside object have method calls predefined method 1 argument. after calling method , try print value of property supposed changed, still remains same. how fix this?

var setname = function(yourname){     this.name = "your name " + yourname; };  // create object called `human` var human = {     name: "nothing here yet",     sethumanname: function(name) {         setname(name);//name should changed     } };  human.sethumanname("emeka"); console.log(human.name); //this not print new value of name 

just use

var human = {     name: "nothing here yet",     sethumanname: setname // no invocation, assigning function }; 

for explicitly invoking arbitrary functions on object (so this keyword set object) use call method of function.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -