2013-05-07 13 views
5

ho il seguente codiceAggiungere la funzione di opporsi

var PROMO = PROMO || {}; 

PROMO.Base = (function() { 
    var _self = this; 

    var Init = function() { 
    WireEvents(); 
    }; 

    var WireEvents = function() { 
    //wire up events 
    }; 

}()); 

Nello stesso file ho il codice per chiamare la funzione di cui sopra

Sto cercando di arrivare a un punto finale in cui posso usare il codice seguente

$(document).ready(function() { 
    PROMO.Base.Init(); 
}); 

questo dà l'errore

Cannot call method 'Init' of undefined 

Ora so che ci sono molti modi per scrivere javascript, ma in questo caso voglio essere in grado di chiamare le mie funzioni, o almeno il metodo Init nel modo mostrato sopra.

+0

È mai effettivamente assegnata la funzione memorizzata in 'Init' a' PROMO.Base.Init' –

risposta

6
var PROMO = PROMO || {}; 

PROMO.Base = (function() { 
    var _self = this; 

    var Init = function() { 
    WireEvents(); 
    }; 

    var WireEvents = function() { 
    //wire up events 
    }; 

    var reveal = { 
     Init: Init   
    }; 

    return reveal; 

}()); 

dovete restituire le funzioni pubblico di fronte. Vedi codice aggiornato.

+0

+1 Questo è un ottimo esempio di chiusura semplice; molto bene. – iGanja

+0

@ alex23 this; non aveva nulla a che fare con il problema in mano, quindi ho ignorato eo non ho prestato molta attenzione. ;) – iGanja

5

Working fiddle con entrambi i modelli, utilizzando IIFE e attribuzione diretta.

L'utilizzo di var rende la definizione privata e la funzione non restituisce nulla. Utilizzare questa:

PROMO.Base = { 
    Init: function() { 
    }, 
    WireEvents: function() { 
    }; 
}; 

Stai avvolgendo la definizione di un IIFE (eseguito immediatamente espressione di funzione). Pertanto al tuo oggetto PROMO.Base verrà assegnato il valore dei ritorni (function(){//blabla})();. Ma la tua funzione non ha un'istruzione return. Di default restituirà undefined.

che è il modo il vostro PROMO.Base sarà undefined e si ottiene questo:

Cannot call method 'Init' of undefined 

Se davvero si vuole che IIFE:

var PROMO = PROMO || {}; 
// NEVER use _self = this inside static functions, it's very dangerous. 
// Can also be very misleading, since the this object doesn't point to the same reference. 
// It can be easily changed with Function.prototype.call and Function.prototype.apply 
PROMO.Base = (function() { 

    _PROMO = { 
     Init : function() { 
      document.body.innerHTML += "itworks"; 
     }, 
     WireEvents : function() { 
    //wire up events 
     } 
    } 
    return _PROMO; 
}()); 
PROMO.Base.Init(); 

Aggiornamento

Il modello migliore e più facile è semplicemente assegnare le funzioni a PROMO.Base. Dully nota che non si dovrebbero capitalizzare le funzioni statiche, ma solo i costruttori. Quindi, se qualcosa non deve essere istanziato, non chiamarlo Init, dovrebbe essere init. Questa è la convenzione.

var PROMO = {}; 
PROMO.Base = {}; 
PROMO.Base.init = function() { 
    console.log("this works"); 
}; 
PROMO.Base.wireEvents = function() { 
    console.log("this is a static function too"); 
}; 
+0

+1 non mi ; questo è il mio metodo preferito, anche se il metodo di Drew è valido. – iGanja

+0

...e ora sei andato e ha aggiunto il ritorno. di nuovo, ritengo che entrambi i metodi siano validi. – iGanja

+0

un'ottima risposta – iGanja

0

È possibile allegare all'oggetto window come ...

window.PROMO = (function($, _){ 

// this will access PROMO.Base 
PROMO.Base = { 
    // inner functions here 
    Init:{} 
}; 

})(jQuery, _); 

Poi caricarlo come si fa.

O se si dipende da jQuery

(function($){ 
    var PROMO = { 
     // inner functions 
     Init: function(){}, 
     WireEvents: function(){} 
    }; 

    $.PROMO = PROMO; 
})(jQuery); 

Su DOM pronto

jQuery(function ($) { 
    var promo = $.PROMO || undefined; 
    promo.Base.Init(); 
}); 
Problemi correlati