Following by parts:
First, its objeto
variable in $(document).ready(...
does not have var
logo declared in global scope (reusable in all functions within the script), this variable will receive new Obj1()
, which is a function in scope.
Inside Obj1()
exists:
var minhaFunc = function(){
document.write(texto);
}
Let's keep her quiet, we'll get back to her later.
At another point in the code of the function Obj1()
, there is a new declaration of the variable objeto
, but this time using var
, that is, it is restricted to this function, any call to it < strong> within this function will receive, instead of Obj1()
or Obj2()
.
Soon after we have:
this.passa = function(){
objeto.setFunc(minhaFunc);
objeto.exec();
}
The function passa()
, calls var objeto
, which at this point refers to the function Obj2()
, function which has another "subfunction" to setFunc()
:
this.setFunc = function(func){
minhaOutraFunc = func;
}
This function receives as a parameter / argument another function that will be stored in var minhaOutraFunc
, knowing that it belongs to Obj2()
. But in Obj1()
we pass it minhaFunc()
:
objeto.setFunc(minhaFunc);
That is, var minhaOutraFunc
in Obj2()
now has minhaFunc()
, already said previously ("we'll get back to it later"). This will print on the "First Object" screen.
But within the passar()
function, there is also:
objeto.exec();
The exec()
in Obj2()
is a function that runs var minhaOutraFunc
, remember that it has minhaFunc()
that belongs to Obj1()
text? Yeah. Look:
this.exec = function(){
minhaOutraFunc.call();
}
The big problem is that the% w of% within each Obj is declared inside each constraint function, due to the use of var texto
.
So when executing a function within the scope of each function, the text variable will match the function in question.
Exactly so, the text of the function var
is printed on the screen.
But what if I wanted, in that case, to write the text of Obj2 ()
It suffices to declare the variable Obj1()
, in the global scope, that is, without the use of texto
. 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>
Thishappensbecausewithinthevar
thelastexecutionisthefunction$(document).ready(...
ofpassa()
.Andthelastexecutionwithinthisfunction,isthecallofwritingonthescreen,aswhatwillbewrittenisthevariableObj1()
andthelastdeclarationofitwithintheglobalscope(sincethistimeitwasdeclaredwithoutthetexto
)wasdoneinvar
,whatisinthatfunctionwillbeprinted.
Andanotherthing,avariablehoweveryourcall/usageisnotwithObj2()
,tobeconsideredglobal,shouldbedeclaredyeswithvar
,butinthescopeinwhichyouwanttoreuse,globally,orinafunction.Thisalsoappliestothevar
variableatthebeginningofyourcode.
Andwhydiditworkintheprevioussituation?
Thejavascriptwilltrytogiveyouthesmallestpossibleamountoferrors,especiallywhenwetalkaboutreusingvariables.That'swhywedonothavetodetermineitstype,sowecandothingslikethis:
"5" * 8; // 40
Relevant information throughout this text: Whenever you run codes that involve variables, their execution will take into account the last statement of these variables.