+ 1
What the difference between this
function person(name, age) { this.name= name; this.age = age; this.yearOfBirth = bornYear; } function bornYear() { return 2016 - this.age; } var p = new person("A", 22); document.write(p.yearOfBirth()); And this 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"); document.write(p.name); .which is best to use
1 Antwort
+ 1
The top one isn't going to work how you would expect it to. The 'this.age' in the bornYear function is a variable that only the bornYear function knows about, unless there's some "this" JavaScript magic that I don't know about yet. I assume you were asking about the difference in the functions.