6
MyLibrary.MyModule =
        (
            function initialise() {
                this.id = id;
                this.c = document.getElementById(id);
                this.ctx = this.c.getContext('2d');

                this.properties = {
                    setup: {
                        backgroundColour: options.setup.backgroundColour || 'black'
                    },

                    scale: {
                        show: options.scale.show || true,
                        colour: options.scale.color || 'white'
                    },
                }
                console.log(properties.setup.baseFontSize);
            }
        )(id, options);

I'm calling this code using

new MyLibrary.MyModule('c',options);

but the 'id' and options seems to be not defined.
can someone help?

2
  • I started with something like this(without closure) and then I thought I'd try to refactor the function into a closure. MyLibrary.MyModule = function initialise(id,options) { this.id = id;.. } Commented Jan 20, 2011 at 18:55
  • 1
    With your refactor, you're invoking the constructor instead of assigning it. If you want it scoped with some default values, you should have the function you're invoking return your constructor. Like: MyLibrary.MyModule = ( function( _id,_options) {return function initialise(id,options) { /* your function body */ }; })(id,options);, though I'm not sure if this was your reason for doing it that way. Commented Jan 20, 2011 at 19:03

3 Answers 3

7

As written, I don't think that's going to do anything like what you want. You're initializing "MyLibrary.MyModule" to be basically nothing; there's no return value from that "initialize" function, and you're calling it as if it did have one.

I can't tell what you're trying to do, but:

MyLibrary.MyModule = (function whatever() { /* ... */ })(id, options);

means, "call the function whatever with an argument list consisting of the values of variable "id" and variable "options", and then set the property "MyModule" on the object referred to by "MyLibrary" to whatever value is returned from that function call."

When the smoke clears, "MyLibrary.MyModule" won't be a function, as far as I can tell. Perhaps if you explain what you want it to mean, then someone can help fix it.

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

Comments

5

Your MyLibrary.MyModule itself is undefined. This is because you're invoking an anonymous function with no return value to assign to it.

I assume you meant to do this instead:

MyLibrary.MyModule = function initialise(id, options) {
            this.id = id;
            this.c = document.getElementById(id);
            this.ctx = this.c.getContext('2d');

            this.properties = {
                setup: {
                    backgroundColour: options.setup.backgroundColour || 'black'
                },

                scale: {
                    show: options.scale.show || true,
                    colour: options.scale.color || 'white'
                },
            }
            console.log(properties.setup.baseFontSize);
        };

Now you can do:

var inst = new MyLibrary.MyModule('c',options);

...and the 'c' and options will be received as arguments to the constructor.

If your purpose for the immediately invoked function expression was to close around some default value that the constructor could reference, then the IIFE would need to return a function that references that value.

Comments

0

You want something like this:

MyLibrary.MyModule = function(id, options) {
    this.id = id;
    this.c = document.getElementById(id);
    this.ctx = this.c.getContext('2d');

    this.properties = {
        setup: {
            backgroundColour: options.setup.backgroundColour || 'black'
        },

        scale: {
            show: options.scale.show || true,
            colour: options.scale.color || 'white'
        },
    }
};

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.