Tutorial by Examples

var person = { name: 'John Doe', age: 42, gender: 'male', bio: function() { console.log('My name is ' + this.name); } }; person.bio(); // logs "My name is John Doe" var bio = person.bio; bio(); // logs "My name is undefined" In the above code, person.bi...
One common pitfall is to try and use this in a nested function or an object, where the context has been lost. document.getElementById('myAJAXButton').onclick = function(){ makeAJAXRequest(function(result){ if (result) { // success this.className = 'success'; } }) }...
5.1 Every function has a bind method, which will create a wrapped function that will call it with the correct context. See here for more information. var monitor = { threshold: 5, check: function(value) { if (value > this.threshold) { this.display("Value is too high!&quot...
When using a function as a constructor, it has a special this binding, which refers to the newly created object: function Cat(name) { this.name = name; this.sound = "Meow"; } var cat = new Cat("Tom"); // is a Cat object cat.sound; // Returns "Meow" var ca...

Page 1 of 1