+ 2
Why in changeName statement we use only function (name) and not person (name) ?
function person(name, age) { this.name = name; this.age = age; this.changeName = function (name) { this.name = name; } } var p = new person("David", 21); p.changeName("John");
1 Resposta
+ 2
Firstly, function(name) { this.lastname = name; } is a function expression.
Thus, the function is originally stored in a constructor-local variable. By storing it in a property of this, it become a method of the constructed object, instead of just a function that is local to the constructor.