(function(){
var privateSomething = "Boom!";
var fn = function(){}
fn.addFunc = function(obj) {
alert('Yeah i can do this: '+privateSomething);
for(var i in obj) fn[i] = obj[i];
}
window.fn=fn;
})();
fn.addFunc({
whereAmI:function()
{
alert('Nope I\'ll get an error here: '+privateSomething);
}
});
fn.whereAmI();
¿Por qué no puede whereAmI() acceder a privateSomething? y ¿cómo coloco whereAmI() en el mismo contexto que addFunc()?Javascript Ámbito en función inyectada
'this' es una palabra clave, no una variable. No tiene nada que ver con el alcance. – Esailija
Gotcha. Entonces, no sigas hablando de esa variable, ¿eh? – lilturtle
'this' es una referencia a un objeto (o' undefined'), y se resuelve dinámicamente. – 1983