0

I am essentially trying to set a function's prototype within the function itself. The purpose is to hide many functions inside one public function, yet keep the functionality.

Here is the non-working code in a jsFiddle. The intended behaviour is displaying "Greetings!" and "Take me to your leader."

The code can be 'fixed' by moving the Outer.prototype lines and the Extensions function out of the Outer function to the global scope. However, this means the Extensions function is public and free for direct use, which it shouldn't be.

What would be a good way to achieve this 'private' behaviour?

1 Answer 1

1

This does not work because you are overriding the prototype. When you call new Outer, then this will inherit from the current Outer.prototype. You are then overriding Object.prototype inside the function, but it does not affect the currently created instance, only future ones.
You'd have to extend the existing prototype.

But if you want hide certain functions, use a self-invoking function:

var Outer = (function() {

    function Extensions() {
    }
    Extensions.protoype.Greet = function () {
        alert(this.Greetings);
    }

    function Inner() {
        Extensions.call(this);
        this.Greetings = "Take me to your leader.";
    }
    Inner.prototype = Object.create(Extensions.prototype);
    Inner.prototype.constructor = Inner;

    function Outer() {
        Extensions.call(this);
        this.Greetings = "Greetings!";
    }
    Outer.prototype = Object.create(Extensions.prototype);
    Outer.prototype.constructor = Outer;

    Outer.prototype.getInner = function() {
        return new Inner();
    };

    return Outer;

}());

Here, Extensions is defined inside the immediately executed function. Therefore it is not accessible from the outside. Only Outer can be accessed since you return it from that function.

See this answer for a very good explanation of inheritance done right in JavaScript.

Sign up to request clarification or add additional context in comments.

7 Comments

Cool. What about the Inner function? The Extensions.Greet function must also be able to be called on an Inner object.
You can do it exactly the same way. Define it inside the closure and return a new instance from the getInner method.
Could you add that to the code so I can see it? It's a little hard to see it in my mind's eye. Especially since I'm still trying to grok the fancy stuff like call.
Yeah, I knew that, just hadn't seen it in action much. You gave me the idea of encapsulating everything into a function like you did, but without the extra closure stuff, just to hide the functions. Is that a bad idea? If so, why?
|

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.