Going by parts:
First, your variable is objeto
present in the $(document).ready(...
does not possess the var
logo it declared in the global scope (reusable in all functions within the script), this variable will receive the new Obj1()
, which is a function in the scope.
Within the Obj1()
exists:
var minhaFunc = function(){
document.write(texto);
}
Let’s keep her quiet, we’ll come back to her later.
At another time the function code Obj1()
, there is a new variable declaration objeto
, but this time with the use of var
, that is, is restricted to that function, any call to it within that function will receive, instead of Obj1()
the Obj2()
.
Right after we have:
this.passa = function(){
objeto.setFunc(minhaFunc);
objeto.exec();
}
The function passa()
, flame to var objeto
, which at present refers to the function Obj2()
, function which has another "subfunction" to setFunc()
:
this.setFunc = function(func){
minhaOutraFunc = func;
}
This function takes as a parameter/argument another Function which will be stored in the var minhaOutraFunc
, knowing that it belongs to Obj2()
. But in the Obj1()
we passed to her the minhaFunc()
:
objeto.setFunc(minhaFunc);
That is, the var minhaOutraFunc
in the Obj2()
now owns the minhaFunc()
, previously said ("we will return to it later"). This will print on the screen "First Object".
But within the function passar()
, there is also:
objeto.exec();
The exec()
in the Obj2()
is a function that executes the var minhaOutraFunc
, recalls that this possesses the minhaFunc()
that belongs to the Obj1()
and who writes this text on the screen? Yes. Look:
this.exec = function(){
minhaOutraFunc.call();
}
The big problem was because the var texto
within each Obj, is declared within each function restrictive, due to the use of var
.
So when performing a function within the scope of each function, the text variable will match the function in question.
Exactly for this reason, the text of the function is printed on the screen Obj1()
.
But what if I wanted, in this case, to write the text of Obj2()
Simply declare, the variable texto
, in the overall scope, i.e., without the use of var
. Look:
var objeto;
$(document).ready(function(){
objeto = new Obj1();
objeto.passa();
});
var texto;
function Obj1(){
texto = "Primeiro Objeto";
var minhaFunc = function(){
document.write(texto);
}
var objeto = new Obj2();
this.passa = function(){
objeto.setFunc(minhaFunc);
objeto.exec();
}
}
function Obj2(){
texto = "Segundo Objeto";
var minhaOutraFunc;
this.setFunc = function(func){
minhaOutraFunc = func;
}
this.exec = function(){
minhaOutraFunc.call();
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
This happens because inside the $(document).ready(...
the last execution is that of the function passa()
of Obj1()
. And the last execution within that function, is the call of the writing on the screen, as what will be written is the variable texto
and her last statement within the overall scope (since this time it was declared without the var
) was made in the Obj2()
, what is in that function will be printed.
And another thing, a variable however much your call/use is not with var
, to be considered global, must be declared yes with the var
, however in the scope where you want to reuse, in the global, or in a function. This also applies to the variable objeto
at the beginning of your code.
And why it worked in the previous situation?
Javascript will try to give you as few errors as possible, especially when we talk about reusing variables. That’s why we don’t need to determine your type, why we can do things like this:
"5" * 8; // 40
Relevant information within all this text: when executing codes involving variables, their execution will take into account the last declaration of these variables.
More specifically.. What would you want with this function? A method that prints something straight, like the first function?
– Samir Braga
I imagined that as the function is passed to the second object, it would print the "text" variable of the second object.
– Carlos
Um.. I get it.. There are some explanations for this. I will try to put them together in one answer :)
– Samir Braga