This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- this is set to the DOM element --> | |
<a href="#" class="myclass another" id="1" onclick="alert(this.id + ' ' + this.className)">DOM element</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Fn(a, b) { return a + b + this; }; | |
// implicit this = 1 | |
console.log(Fn.call(1, 2, 3)); //6 | |
console.log(Fn.apply(1, [2, 3])); //6 | |
//note: if null is passed this = window |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//constructor function | |
function Person(id) { | |
//new keyword implicit this = {}; | |
this.id = id; | |
//new keyword implicit return this; | |
}; | |
//this points to new object | |
var person = new Person(1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// object scope | |
var scope = { | |
number: 7, | |
Fn: function() { | |
return this.number; | |
} | |
}; | |
// this points to scope (not window) | |
console.log(scope.Fn()); // 7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var number = 7; | |
console.log(this.number === window.number); // true | |
… | |
another = 8; // global variable | |
console.log(this.another === window.another); // true | |
… | |
//within a function this is set to window | |
function Fn() { | |
return this; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//creation separated from initialisation | |
var order = {}; | |
… | |
order.id = 1; | |
… | |
order.customer = {}; //ups I did it again | |
order.customer.id = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//creation and initialisation together | |
var order = { | |
id: 1, | |
customer: { | |
id: 1 | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Using standard function definition | |
//note: convention is to use initial capital letter | |
function Order(id) { | |
//new keyword implicit code: var this = {}; | |
this.id = id; | |
//implicit: return this; | |
}; | |
… | |
var order = new Order(1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Using an anonymous function reference | |
var Order = function(id) { | |
//new keyword implicit code: var this = {}; | |
this.id = id; | |
//implicit: return this; | |
}; | |
… | |
var order = new Order(1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Using a new object | |
var Order = function(id) { | |
//we ignore ‘this’ altogether… it’s evil right? | |
var that = {}; | |
that.id = id; | |
return that; | |
}; | |
… | |
var order = new Order(1); |