2

I have JavaScript variable as a literal:

var global = {
    getTime : function() {
        var currentDate = new Date();
        return currentDate.getTime();
    }
};

And I wish to extend this literals with other different functions, which are going to be created as variables:

var doSomething = function(param){
    $("#" + param).hide();
    return "hidden";
}


How can I extend my literal with a new variable, which holds a function?!
At the end I wish to use this in such a way:

alert( global.doSomething("element_id") );

3 Answers 3

4

To extend your global variable with the method doSomething, you should just do this:

global.doSomething = doSomething;

http://jsfiddle.net/nslr/nADQW/

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

Comments

4
var global = {
    dothis: function() {
        alert('this');
    }
}

var that = function() {
    alert('that');
};

var global2 = {
    doSomething: that
};

$.extend(global, global2);


$('#test').click(function() {
    global.doSomething();
});

Comments

0
global.doSomething = function(param){

or

var doSomething = function(param){ ...
global.doSomething = doSomething;

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.