Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Obj = function(a){ this.a = a;};
- Obj.prototype.test = function(b){ return ++b; };
- var Obj2 = function(){
- //actual function returned by call
- function MyConstructor( a ){ this.a = a; }
- //a function we'll use on this object
- function test(b){ return ++b; }
- //get the prototypa reference
- var _p = MyConstructor.prototype;
- //assign as constructor
- _p.constructor = MyConstructor;
- //assign the function to the prototype
- MyConstructor.prototype.test = test;
- //now Obj2 = Myconstructor
- return MyConstructor;
- }();// <- call
- function main(){
- var i, o, v, nb = 10000000;
- console.time( "obj1" );
- v = 0;
- for(i=0;i<nb;i++)
- {
- o = new Obj( "a" );
- v = o.test(v);
- }
- console.timeEnd( "obj1" );
- console.log( "v = ", v );
- console.time( "obj2" );
- v = 0;
- for(i=0;i<nb;i++)
- {
- o = new Obj2( "a" );
- v = o.test(v);
- }
- console.timeEnd( "obj2" );
- console.log( "v = ", v );
- }
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement