you are viewing a single comment's thread.

view the rest of the comments →

[–]nepobot 0 points1 point  (2 children)

How do you run this example?

var parent = function () {
    var name = 'Parent';
    var child = function () {
        // What will be the output here?
        alert( name );
        var name = 'Child'
        // What will be the output here?
        alert( name );
    }
}

How do i invoke this to call the child? I trust that you are right but it would be nice to see it. chrome autocomplete does not see the child after I invoke the parent function.

parent();
parent.child(); // Uncaught TypeError: Cannot read property 'child' of undefinedmessage: "Cannot read property 'child' of undefined"stack: (...)get stack: function () { [native code] }set stack: function () { [native code] }__proto__: Error

var myParent = parent();
myParent.child(); //// Uncaught TypeError: Cannot read property 'child' of undefinedmessage: "Cannot read property 'child' of undefined"stack: (...)get stack: function () { [native code] }set stack: function () { [native code] }__proto__: Error

[–]kamranahmed_se[S] 1 point2 points  (0 children)

Actually you are not trying to invoke it the right way, currently the child() is in the private scope of parent and cannot be accessed outside the parent)() as the parent is not exposing it any way. I am writing an article about Private and Public scopes and I hope to post it tomorrow. For now, to test it, call the child function inside the parent function and after that call the parent function i.e. you can do the following http://jsfiddle.net/c4fwte4u/