0

I'm still confused about this part of the closure although I read about it a lot (also here in the site). Took the code from here: http://www.crockford.com/javascript/private.html

So what is the different between this:
function Container(param) {
    this.member = param;
}
...And this -
function Container(param) {
    var member = param;
     // and also in case it's without "var", I.e. global
}

Please explain what happens when you're creating the obj for each case -

var myContainer = new Container('abc');

Also - What are the access differences for the parameter from the object? And can you give example for a function as a parameter and a returning function?

Thanks a lot!

FED
  • 319
  • 1
  • 2
  • 12
  • Do you know what `var b` does? It simply creates a local variable `b`. `a.b = ..` on the other side assigns a property `b` to object `a`. Two completely different things. – Felix Kling Mar 01 '14 at 00:06

2 Answers2

0

Here is my opinion: When you use the new to create an object through function,the variable initial by var is local variable:

function Test(){
    var name = "John";
    this.getName = function(){
        return name;   
    }
}
var obj1 = new Test();
console.log(obj1.name);  //undefined
console.log(obj1.getName());  //John

That means you can't read the variable directly outside the function.This is like private variable in Java or c++;

but when you use this.name = "John", this is a different situation:

function Test2(){
    this.name = "John";
}
var obj2 = new Test2();
console.log(obj2.name)  //"John"

you can read the variable directly, this is like "public" variable in java or c++.

Hope this can work for you. : )

Tyler.z.yang
  • 2,402
  • 1
  • 18
  • 31
  • Thanks a lot!! it does help very much :) Could you perhaps show me how it works with prototype? (the secured vs. unsecured access to name, for example) – FED Mar 02 '14 at 00:28
  • Sorry, it seems my answer is too long in this comment mode.So I just answer in the Answer mode. : ) – Tyler.z.yang Mar 03 '14 at 02:23
0

In function Test, when we declare a variable obj1 with new Test. It have been created through the contructor function which is called "Test".

This process is like we call a function in a normal way. Of course that make a local variable "name". When we decleared a function expression which is called "this.getName", that just mean an expression to function "Test".

But when we call the "new Test".It return a object which have a key-value call "getName:function{}". When we call "obj1.getName", it return the variable "name". But it cannot find the getName's local variable "name", so it will search its parent's scope to find if there is a variable "name",it will not stop until we get it or just return "undefined". That make an "quote" to keep the variable "name" in memory, but we just can get it through the function "getName".

For all of this, we make a private variable in our function "Test". : ) Hope this can help.

Tyler.z.yang
  • 2,402
  • 1
  • 18
  • 31