Skip to content

Instantly share code, notes, and snippets.

@anasnakawa
Last active August 29, 2015 14:01
Show Gist options
  • Save anasnakawa/b0bc4ee1149fefbd23e6 to your computer and use it in GitHub Desktop.
Save anasnakawa/b0bc4ee1149fefbd23e6 to your computer and use it in GitHub Desktop.
function Car() {
this.array = [];
this.log = function() {}
}
Car.prototype.something = function() {
var self = this; // <- fine
this.array.forEach( function(item) {
self.log( item );
});
}
var self; // <- bad
function Car() {
this.array = [];
this.log = function() {}
}
Car.prototype.something = function() {
self = this;
this.array.forEach( function(item) {
self.log( item );
});
}
(function() {
var self; // <- really bad
function Car() {
this.array = [];
this.log = function() {}
}
Car.prototype.something = function() {
self = this;
this.array.forEach( function(item) {
self.log( item );
});
}
window.Car = Car;
})();
(function() {
function Car() {
this.array = [];
this.log = function() {}
}
Car.prototype.something = function() {
self = this; // omiting var in non `strict-mode`, will let variable became global
this.array.forEach( function(item) {
self.log( item );
});
}
window.Car = Car;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment