Javascript: prototyping a function with arguments for performance

In John Resig's article on Simple "Class" in Javascript , he states:

"... if you have a frequent access function (returning an object) with which you want to interact, then it is in your interest that the properties of the object are in the prototype chain and create an instance., in the code:"

// Very fast
function User(){}
User.prototype = { /* Lots of properties ... */ };
// Very slow
function User(){
    return { /* Lots of properties */ };
}

I would like to apply this to a function like the following (what happens inside the class declaration)

//...
this.get_subscription = function(args)
{
    return {
        property_one: "value",
        property_two: {
            sub_prop: args.something
        }
    }
}

but I have no idea where to put the arguments. If i do

this.get_subscription = function(args) {}
this.get_subscription.prototype = {
    property_one: "value"
    //...
}

he will say that args is undefined. I tried several options, none of which work. How to do this correctly so as not to put arguments in the scope of the parent class?

+3
2

, "--prototype" -idea: - ( , ), , , ; " " ( , ).

, . , , , ( this.Subscription). , .

, :

function Class() {
   this.Subscription = function(args) {
       this.propertyTwo = {
           subprop: args.something
       };
   };
   this.Subscription.prototype = {
       property_one: "value",
       property_two: {}
   };
}
// a method callable without the "new" operator:
Class.prototype.get_subscription = function(args) {
     return new this.Subscription(args);
}

:

var object = new Class();
var sub = new object.Subscription();
var sub2 = new object.Subscription();
// the promised boost is less memory usage for:
sub.propertyOne === object.Subscription.prototype.propertyOne === sub2.propertyOne;

Prototypal inheritance Crockford - // propertyTwo -Object (s).

+1

, prototype. , . , property_two args, , !

,

this.get_subscription = function(args) {}
this.get_subscription.prototype = {
    property_one: "value"
    //...
}

get_subscription, . args, , - args .

, javascript : 2D-.

function Point(x, y) {
    /*  In constructor:
     *  initialize fields that are related to the constructor parameters
     *  and are different for different instances
     */
    this.x = x;
    this.y = y;
}

// in prototype, define fields that are  common to all instances
Point.prototype.RED = '#FF0000';

/*  in prototype, also define the methods; as they can use the "this" keyword,
 *  they can reference their own instance, so you don't have to include them
 *  in each instance manually
 */

Point.prototype.toString = function() { return '[' + this.x + ', ' + this.y + ']'; };
+3

All Articles