Created
May 31, 2015 19:28
-
-
Save rootid/6eeaaa2b1b74249ed3d9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//Augmenting the JS objects (NOTE : in JS function are objects) | |
//Ugly | |
function makePerson(first, last) { | |
return { | |
first: first, | |
last: last | |
}; | |
} | |
function personFullName(person) { | |
return person.first + ' ' + person.last; | |
} | |
function personFullNameReversed(person) { | |
return person.last + ', ' + person.first; | |
} | |
s = makePerson("Simon", "Willison"); | |
console.log(personFullName(s) ); // "Simon Willison" | |
var tmp = personFullName(s) | |
console.log(tmp); // "Simon Willison" | |
console.log(personFullNameReversed(s) ); //"Willison, Simon" | |
//Bad | |
// Why Every time we create a person object we are creating two brand new function objects within it | |
function makePerson(first, last) { | |
return { | |
first: first, | |
last: last, | |
fullName: function() { | |
return this.first + ' ' + this.last; | |
}, | |
fullNameReversed: function() { | |
return this.last + ', ' + this.first; | |
} | |
}; | |
} | |
s = makePerson("Simon", "Willison") | |
console.log(s.fullName() ); // "Simon Willison" | |
console.log(s.fullNameReversed() ); // "Willison, Simon" | |
//Bad1 Constructor inovocation pattern | |
// Why Every time we create a person object we are creating two brand new function objects within it | |
function Person(first, last) { | |
this.first = first; | |
this.last = last; | |
this.fullName = function() { | |
return this.first + ' ' + this.last; | |
}; | |
this.fullNameReversed = function() { | |
return this.last + ', ' + this.first; | |
}; | |
} | |
var s = new Person("Simon", "Willison"); | |
//Bad2 | |
//No extra function object creation | |
function personFullName() { | |
return this.first + ' ' + this.last; | |
} | |
function personFullNameReversed() { | |
return this.last + ', ' + this.first; | |
} | |
function Person(first, last) { | |
this.first = first; | |
this.last = last; | |
this.fullName = personFullName; | |
this.fullNameReversed = personFullNameReversed; | |
} | |
//Good | |
//Person.prototype is an object shared by all instances of Person. It forms part of a lookup chain (that has a special name, "prototype chain"): any time you attempt to access a property of Person that isn't set, JavaScript will check Person.prototype to see if that property exists there instead. As a result, anything assigned to Person.prototype becomes available to all instances of that constructor via the this object. | |
function Person(first, last) { | |
this.first = first; | |
this.last = last; | |
} | |
Person.prototype.fullName = function() { | |
return this.first + ' ' + this.last; | |
}; | |
Person.prototype.fullNameReversed = function() { | |
return this.last + ', ' + this.first; | |
}; | |
//Using string exmple | |
String.prototype.reversed = function() { | |
var r = ""; | |
for (var i = this.length - 1; i >= 0; i--) { | |
r += this[i]; | |
} | |
return r; | |
}; | |
var s = "Hello world!"; | |
console.log(s.reversed()) | |
String.method('reverse',function() { | |
var r = ""; | |
for (var i = this.length - 1; i >= 0; i--) { | |
r += this[i]; | |
} | |
return r; | |
} ); | |
console.log(s.reverse()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment