deepak
deepak

Reputation: 113

Do functions attached to the prototype property not have closure

I am trying to figure out how I can add methods to a constructor after I have created it.
In my code below, I cannot use Person's prototype property to add a new public method which has access to Person's vars. (Do the functions attached to the prototype property not close over the vars in the main function).
Unlike the first way, the second way works - Person 2. seems like these are called privileged methods -http://www.crockford.com/javascript/private.html.

function Person(name, age){}
Person.prototype.details = function(){ 
    return "name: "+name+", age: "+age;
};

function Person2(name, age){
 this.details = function(){ 
    return "name: "+name+", age: "+age;};
}

var per1 = new Person("jim", 22);
var per2 = new Person2("jack", 28);

per1.details();
//=> ReferenceError: age is not defined
per2.details();
//=> "name: jack, age: 28"

Upvotes: 1

Views: 92

Answers (3)

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76405

Of course not, the function was declared in a scope, different to the scope where the arguments/variables were declared, so JS wouldn't know which variables you're on about. Suppose you had a second closure, or better (well, worse actually) yet: a global variable called name. Which one would JS pick?

Here's an example for you:

function MyObject(name)
{
    var localVar = 'foobar';
    this.evilMethod = (function(localVar)
    {
        return function()
        {
            console.log('localVar = '+localVar);//=== name
        };
    })(name);
    this.badMethod = function()
    {
        console.log('localVar = '+ localVar);// === 'foobar'
    };
}
var name = 'Global Name';
var anotherClosure = (function(name)
{
    var localVar = name.toLowerCase();
    return function()
    {
        console.log(name);
        console.log(localVar);
    }
})('Bobby');
MyObject.prototype.closureVars = function()
{
    console.log(name);//Global name
    console.log(localVar);//undefined
};

Now first off: this is terrible code, but you get the point: you can have hundreds of variables with the same name, which one JS has to use, might not always be clear.

Giving prototypes access to instance closure variables has other implications, too: you could, for instance change their values, which defeats the point of having a closure in the first place.
But the biggest problem by a country mile would be: multiple instances! If you create a constructor, odds are you're going to instantiate more than 1 object with it. How would that work, if they all share the same prototype?

Just assign the arguments/variables you want to access in the prototype to a property, like FishBasketGordo's example does

Upvotes: 0

FishBasketGordo
FishBasketGordo

Reputation: 23142

No, they do not have closure over the constructor functions vars. They are in a different scope.

// This function is in one scope.
function Person(name, age) {
}

// This statement is in the parent scope, which 
// doesn't have access to child scopes.
Person.prototype.details = function(){ 
    return "name: "+name+", age: "+age;
};

That's the way that "public" functions work in JavaScript. You could make details a privileged function by defining it within the constructor:

function Person(name, age) {
    this.details = function() { 
        return "name: "+name+", age: "+age;
    };
}

Of course, that means that each instance of Person gets it's own copy of the details function.

You could also, as @Chuck suggests, make name and age public members, in which you would have access to them in a prototype function:

function Person(name, age) {
    this.name = name;
    this.age = age;
}

Person.prototype.details = function(){ 
    return "name: " + this.name + ", age: " + this.age;
};

Upvotes: 4

user905127
user905127

Reputation:

No. Typically you would either use the second approach, or set this._name = name; in the constructor and reference it that way in the other method.

Upvotes: 1

Related Questions