1

Je variable JavaScript comme un littéral:Comment étendre le littéral JavaScript (objet) avec une nouvelle variable?

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

Et je tiens à ce littéraux avec d'autres fonctions différentes, qui vont être créées en tant que variables:

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


Comment Je prolonge mon littéral avec une nouvelle variable, qui tient une fonction ?!
A la fin je souhaite utiliser ce de telle manière:

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

Répondre

3

Pour étendre votre variable global avec la méthode doSomething, vous devez simplement faire:

global.doSomething = doSomething; 

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

0
global.doSomething = function(param){ 

ou

var doSomething = function(param){ ... 
global.doSomething = doSomething; 
3
var global = { 
    dothis: function() { 
     alert('this'); 
    } 
} 

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

var global2 = { 
    doSomething: that 
}; 

$.extend(global, global2); 


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