Why my method is not executing what I want it to execute? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why my method is not executing what I want it to execute?

Please answer my question and DO not write an alternative code!!!!!! //I want to change the name James to Aron but it does not change it. //How can I fix it with this syntax ? //What is wrong here? function person (name, age) { this.name = name; this.age = age; this.changeName = methFunc; } function methFunc () { var x = "Aron"; return x = this.name; } var aron = new person ("James") alert (aron.changeName()) //There is another code I wrote with the same syntax that execute what I want: function player (score) { this.score = score this.updateScore = update; } function update () { return 500 + this.score } var winMatch = new player (200) alert (winMatch.updateScore())

15th Mar 2017, 11:38 AM
Waqas Shah
Waqas Shah - avatar
5 Answers
+ 3
sorry, answer is function person (name, age) { this.name = name; this.age = age; this.changeName = methFunc; } function methFunc () { var x = "Aron"; return this.name = x; } var aron = new person ("James") alert (aron.changeName()) in methFunc you set x="Aron" and then set it to "James" and return it
15th Mar 2017, 11:51 AM
Yaroslav Pieskov
Yaroslav Pieskov - avatar
+ 2
@Yaroslav Pieskov: Thank you so much for your help! The right syntax is: this.name = name: so the right code is: var x = "Aron" return this.name = x or return this.name = "Aron"
15th Mar 2017, 12:19 PM
Waqas Shah
Waqas Shah - avatar
+ 2
I could not understand that equality sign " = " is also an operator like the plus sign " + " is an operator. Then why = did not behave like + or vice versa. I mean in the second example the this.score was after 500 + but it worked. May be the this.name = name syntax is just for assignment operator "="
15th Mar 2017, 12:47 PM
Waqas Shah
Waqas Shah - avatar
+ 1
yes. return this.name="Aron"
15th Mar 2017, 12:20 PM
Yaroslav Pieskov
Yaroslav Pieskov - avatar
+ 1
@Yaroslav Pieskov Thank you so much! If you need any graphic related work I will do for you as thanks in $0.0 :) Instagram @Waqas.Shah
15th Mar 2017, 12:24 PM
Waqas Shah
Waqas Shah - avatar