Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var c = {
- name: 'The c object',
- log: function(){
- this.name = 'Updated c object';
- console.log(this);
- var setName = function(newname){
- // this now points to the global object
- this.name = newname;
- };
- setName('Updated again! The c object');
- console.log(this);
- }
- }
- c.log();
- // To fix it we insert a new variable self and give it a reference to this
- var d = {
- name: 'The c object',
- log: function(){
- var self = this;
- self.name = 'Updated c object';
- console.log(this);
- var setName = function(newname){
- // this now points to the global object
- self.name = newname;
- };
- setName('Updated again! The c object');
- console.log(self);
- }
- }
- d.log();
Add Comment
Please, Sign In to add comment