Difference between revisions of "This Keyword"

From rbachwiki
Jump to navigation Jump to search
(Created page with "==Using this keyword== '''By using just "name" it will call the global "name" as opposed to the local name''' <pre> var name = "robert"; var a = { name: "John", greet: fun...")
 
Line 6: Line 6:
   name: "John",
   name: "John",
   greet: function(){
   greet: function(){
     console.log('My name is '+ '''name''');
     console.log('My name is '+ name);
   }
   }
};
};
Line 18: Line 18:
   name: "John",
   name: "John",
   greet: function(){
   greet: function(){
     console.log('My name is '+ '''this.name''');
     console.log('My name is '+ this.name);
   }
   }
};
};
a.greet();
a.greet();
</pre>
</pre>

Revision as of 17:05, 28 October 2016

Using this keyword

By using just "name" it will call the global "name" as opposed to the local name

var name = "robert";
var a = {
  name: "John",
  greet: function(){
    console.log('My name is '+ name);
  }
};
a.greet();

By using the this.name it will call the local scope variable

var name = "robert";
var a = {
  name: "John",
  greet: function(){
    console.log('My name is '+ this.name);
  }
};
a.greet();