This link has been bookmarked by 36 people . It was first bookmarked on 07 Dec 2007, by lei zhou.
-
10 May 12
-
15 Feb 12
-
10 Nov 11
-
23 Mar 11
tyoc213function makeClass(){
return function(args){
if ( this instanceof arguments.callee ) {
if ( typeof this.init == "function" )
this.init.apply( this, args.callee ? args : arguments );
} else
return new arguments.callee( arguments );
};
}-
function User(first, last){
if ( this instanceof User ) {
this.name = first + " " + last;
} else
return new User(first, last);
}
-
-
16 Nov 10
-
11 Aug 10
-
Finally, a tangible advantage. Instantiating a function with a bunch of prototype properties is very, very, fast. It completely blows the Module pattern, and similar, out of the water. Thus, if you have a frequently-accessed function (returning an object) that you want people to interact with, then it's to your advantage to have the object properties be in the prototype chain and instantiate it. Here it is, in code:
// Very fast
function User(){}
User.prototype = { /* Lots of properties ... */ };// Very slow
function User(){
return { /* Lots of properties */ };
}
-
-
27 Mar 08
-
12 Jan 08
-
05 Jan 08
-
03 Jan 08
-
20 Dec 07
-
19 Dec 07
-
16 Dec 07
-
10 Dec 07
-
08 Dec 07
-
07 Dec 07
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.