http://www.scribd.com/doc/48750547/AJS
Nice presentation touches main concept of the language.
Monday, March 11, 2013
Saturday, March 9, 2013
Function Literal vs Function definition
Function Literal
var Class = function () {};
vs
Function Definition
Function Definition
function Class () {};
the former is "hoisted" to the top of the current scope before execution. For the latter, the variable declaration is hoisted, but not the assignment. For example:
// Error, fn is called before the function is assigned!
fn();
var fn = function () { alert("test!"); }
// Works as expected: the fn2 declaration is hoisted above the call
fn2();
function fn2() { alert("test!"); }
http://stackoverflow.com/questions/4508313/advantages-of-using-prototype-vs-defining-methods-straight-in-the-constructor
Adding methods to a class vs adding methods to a class's prototype
https://www.quora.com/JavaScript/What-are-advantages-to-adding-methods-to-a-class-vs-adding-methods-to-a-classs-prototype
var myClass = function(prop1, prop2) {
http://stackoverflow.com/questions/4508313/advantages-of-using-prototype-vs-defining-methods-straight-in-the-constructor
Snippet 1 (Adding methods to a class)
var myClass = function(prop1, prop2) {
this.prop1 = prop1;
this.prop2 = prop2;
this.method1 = function() {//blah}
this.method2 = function() {//blahblah}
}
Snippet 2 (Adding methods to a class's prototype)
this.prop1 = prop1;
this.prop2 = prop2;
}
myClass.prototype.method1 = function() {//blah}
myClass.prototype.method2 = function() {//blahblah}
Snippet 1
- Two functions are created for every construction of
myClass
can give you access to private variables and constructor arguments
Snippet 2
- the two functions are created once: when the prototype is filled.
- gives better memory usage
- Methods that inherit via the prototype chain can be changed universally for all instances
- For instance, you can extend the built-in String object by adding a trim function to it and all future instance of that object will share the trim function.
http://stackoverflow.com/questions/4508313/advantages-of-using-prototype-vs-defining-methods-straight-in-the-constructor
Subscribe to:
Posts (Atom)