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.