1

Ok, so I'm using the following skeleton code to create a Javascript Library

var Device = (function(window, document, $) {
    function func_1(){
         return 1;
    }

    function func_2(){
        return 2;
    }

    var internalDevice = {
        func_1: func_1,
        func_2: func_2
    };
    return internalDevice; // expose functionality to the rest of the code
})(window, document, jQuery);

Essentially, I would call my functions like so: Device.func_1();.

I'm looking to add a constructor that will initialize some private variables from the get-go i.e. as soon as the object is created, and without having to make any extra function call(s).

How do I do this?!

Thanks.

0

3 Answers 3

2

May be try this way? private_1 and private_2 can only be accessible through function calls while you can use Device() constructor.

function Device(param1, param2) {
    var private_1= param1;
    var private_2= param2;

    this.func_1= function() {
        return private_1;                
    }

    this.func_2= function() {
        return private_2;            
    }
}

var myDevice = new Device(1, 2);
alert(myob.func_1()); 

or may be like this:

var Device = (function(window, document) {
    var private_1;

    function init(param1) {
      private_1 = param1;
    }

    function func_1(){
         return private_1;
    }

    function func_2(){
        return 2;
    }

    var internalDevice = {
        func_1: func_1,
        func_2: func_2,
        init  : init
    };
    return internalDevice; // expose functionality to the rest of the code
})(window, document);

Device.init(10);
alert(Device.func_1())
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, but I'd rather stick to my original 'motif', do you know how I can achieve this with my current format?
Just added another approach.
Thanks. I figured out a way to do this without calling init. I'll post the answer shortly.
You were asking for a way to initialize the private variables, your answer below is just hard-coding the value...
I was looking for a way to have a constructor initialize values for the private variables, without forcing the user to call a specific init method first.
1

I managed to figure this one out, here's how I did it:

var Device = (function(window, document, $) {
    var var_1 = 10,
    var_2 = 20,
    var_3;

    function init(){
         var_3 = 30;
    }

    function func_1(){
         return 1;
    }

    function func_2(){
         return 2;
    }

    var internalDevice = {
        init: init(),
        func_1: func_1,
        func_2: func_2
    };
    return internalDevice;
})(window, document, jQuery);

So when you call Device.func_2();, the variables would already have been initialized. You can see a live example here: http://jsfiddle.net/CZjYH/11/

I am also going to be implementing Amplify.JS functionality inside the init function as a way of persisting my variables to local or Session storage.

Cheers.

Comments

0
var Device = (function(window, document, $, undefined)
{
    return $.extend(function()
    {
        // constructor
    },
    {
        prototype:
        {
            func_1: function func_1()
            {
                return 1;
            },
            func_2: function func_1()
            {
                return 2;
            }
        }
    });
})
(window, window.document, jQuery);

OR

var Device = (function(window, document, $, undefined)
{
    var DevicePrivate = function()
    {
        // constructor
    };

    DevicePrivate.prototype =
    {
        func_1: function()
        {
            return 1;
        },
        func_2: function()
        {
            return 2;
        }
    };

    return DevicePrivate;
})
(window, window.document, jQuery);

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.