Understanding “this” in Backbone / Javascript

Litos

New Member
I'm trying to understand how to create objects and methods with Backbone. I started with something like this:\[code\]Person = (function () { return Backbone.Model.extend({ defaults: { name: 'jon' }, changeName: function (newName) { console.log(newName); this.name = newName; } });})();var p1 = new Person();console.log(p1.get('name'));p1.changeName("samanatha");console.log(p1.get('name'));\[/code\]What I don't understand is, why doesn't my p1.name property change. I thought it had something to do with "this" in this.name since I'm still trying to grasp how 'this' works, but I think I'm missing something else since \[code\]this.name = newName\[/code\] and \[code\]name=newName\[/code\] both do not work. Thanks!
 
Back
Top