javascript - Working with JS objects and functions -


i'm newbie in javascript , i'm wondering why bob.age still 30 while age 50 when called it. set age 50 in setage assigned 50 this.age, know reference bob.age, age , bob.age should have same value.

enter image description here

// here define our method using "this", before introduce bob var setage = function (newage) {     this.age = newage; }; // make bob var bob = new object(); bob.age = 30; // , down here use method made  // change bob's age 50 here bob.setage = setage(50); 

bob.setage = setage(50);

all doing setting setage property of bob result of calling setage(50) - setage retunrs undefined, you've set bob.setage = undefined. perhaps intended

bob.setage = setage;  bob.setage(50); 

untested, think it's right


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -