0

Up till now my understanding of closures is, if a function returns a function, then the returned function(object) remembers the context of its (lexical) parent function scope and I was thinking of a scenario where a closure can be achieved without implementing a nested function.

The code below is a perfect example of this. What I have done here is:

I have used a constructor to create objects and I have used the parameters passed to it in the method toString() defined inside.

Now I have created two objects and both objects have their separate copies of id, name, madeInYear.

Please note I could have achieved the same result by using productId, productName, productOriginDate but just to see closure, I have written the code snippet like this.

Another interesting thing I have observed is I can access productId but not id. Does that means they are not being created in the context of object ?

function f1(id,name,madeInYear)
{
this.productId=id;
this.productName=name;
this.productOriginDate=madeInYear;

this.toString=function()
{
    console.log("Function Parameters:"+id+" "+name+" "+madeInYear);
    console.log("Objects Method:"+this.productId+" "+this.productName+" "+this.productOriginDate);
}

}

var obj1=new f1(1,"Parker Pen","2001");
var obj2=new f1(2,"Jetter",2000);

obj1.toString();
obj2.toString();
obj1.productId;
obj2.productId;

Output

Function Parameters:1 Parker Pen 2001

Objects Method:1 Parker Pen 2001

Function Parameters:2 Jetter 2000

Objects Method:2 Jetter 2000

1

2
4
  • This is not technically a closure; it's a constructor. Commented Mar 4, 2017 at 20:25
  • what makes you think you can't access id ? your output looks like it's working properly. Commented Mar 4, 2017 at 21:33
  • i can access studentId not id. Commented Mar 6, 2017 at 18:36
  • applologies..its productid Commented Mar 6, 2017 at 18:39

1 Answer 1

2

A closure is defined as accessing local variables from the lexical context where a function was defined, when that function is called outside of that context.

So there has to be a function to create a proper closure. You could replace the outer function with something else that has local variables, which is possible in ES6 with block scoping and let declarations:

{
    let local = 3
    function f() {
        return local
    }
}
console.log(local) // undefined
console.log(f())   // 3
Sign up to request clarification or add additional context in comments.

8 Comments

what if i create five objects. we will surely have 5 copies of id,name,madeinYear. Each object will keep holding the reference of id,name, year apart from its own productid,productName,productMadeInYear. Thus if m not wrong closure says remember the context and here each object will remember the context. Please correct me of i'm wrong
If your constructor changes the value of one of its closure variables, then the next time you call the constructor, it will see the modified value. There is no copying involved. The closure is the original context where the function was defined. The copying happens when you assign values to the object's own properties (this.id, etc) and you're doing that explicitly.
Touffy i think i couldn't able to clarify what i was mentioning.
May be because i used the word copy. What i meant is: when i create 5 objects constructor function will be called 5 times. Each time constructor function is called id,name,madeInYear would be created and thus each object will hold reference to these 3 parameters. Thus each object will be refering to their respective id,name,madeInYear along with their own producttId,productName,productOriginDate. Thus we will have in total 6*5=30 variables.
The three parameters will never be destroyed because I have a reference to them in toString method and thus I'm achieving closure.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.