2

I'm trying to create some isolation in javascript using the module pattern.

but when i run this code:

var mymap = {};

mymap['230'] = (function () {
    privatePageLoad = function(nid, page){
        console.log('PAGE LOAD from 230: '+nid);
    }

    function handleDocummentAdded() {
        console.log("New Document in 230!");
        privatePageLoad(230, 0);
    };

    return {
        newDocument: handleDocummentAdded
    }
})();

mymap['318'] = (function () {
    privatePageLoad = function(nid, page){
        console.log('PAGE LOAD from 318: '+nid);
    }

    function handleDocummentAdded() {
        console.log("New Document in 318!");
        privatePageLoad(318, 0);
    };

    return {
        newDocument: handleDocummentAdded
    }
})();

mymap['230'].newDocument();
mymap['318'].newDocument();

I get the following result:

New Document in 230! (index):29
PAGE LOAD from 318: 230 (index):40
New Document in 318! (index):44
PAGE LOAD from 318: 318 

but i was expecting:

New Document in 230! (index):29
PAGE LOAD from 230: 230 (index):40
New Document in 318! (index):44
PAGE LOAD from 318: 318 

The private method that is called is always the last added to the map... What did i do wrong?

Here is a jsfiddle with it http://jsfiddle.net/jpedro/4xdTA/

2 Answers 2

4

privatePageLoad is not private at all. Add var , otherwise privatePageLoad will be global (i.e. property of the global object (window, module, whatever))

Change your code to something like:

var mymap = {};

mymap['230'] = (function () {
    var privatePageLoad = function(nid, page){
        console.log('PAGE LOAD from 230: '+nid);
    }

    function handleDocummentAdded() {
        console.log("New Document in 230!");
        privatePageLoad(230, 0);
    };

    return {
        newDocument: handleDocummentAdded
    }
})();

mymap['318'] = (function () {
    var privatePageLoad = function(nid, page){
        console.log('PAGE LOAD from 318: '+nid);
    }

    function handleDocummentAdded() {
        console.log("New Document in 318!");
        privatePageLoad(318, 0);
    };

    return {
        newDocument: handleDocummentAdded
    }
})();
Sign up to request clarification or add additional context in comments.

Comments

2

You forgot to declare "privatePageLoad" with var. Thus, it's a global variable.

You could also use a function declaration statement:

mymap['230'] = (function () {
    function privatePageLoad(nid, page){
        console.log('PAGE LOAD from 230: '+nid);
    }

    function handleDocummentAdded() {
        console.log("New Document in 230!");
        privatePageLoad(230, 0);
    };

    return {
        newDocument: handleDocummentAdded
    }
})();

Also, get into the habit of putting

"use strict";

at the top of your code. That would have resulted in an error in this case.

Comments

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.