angularjs - Can function and closures replace classes in Javascript? -
this question has answer here:
- how javascript closures work? 89 answers
i reading angularjs , came across following statement on newsgroup:
i have say, though, more use angular, less interest have in creating classes. classes not javascript at! can pretty need pure functions , closures , don't have worry troublesome "this" , "that".
can explain meant "closures".
closures structure in javascript code within nested functions outer functions scope retained within inner function.
for example:
function outer(x,y){ var t = 1; return function(z){ //x, y, t outer function made available inner function return x + y + z + t; } } var outer1 = outer(1,1); //creating closure, or instance of function in sense alert(outer1(1)); //alerts 4 var outer2 = outer(2,2); alert(outer2(2)); //alerts 7 the simple explanation of closure ecmascript allows inner functions; function definitions , function expressions inside function bodes of other functions. , inner functions allowed access of local variables, parameters , declared inner functions within outer function(s). closure formed when 1 of inner functions made accessible outside of function in contained, may executed after outer function has returned. @ point still has access local variables, parameters , inner function declarations of outer function. local variables, parameter , function declarations (initially) have values had when outer function returned , may interacted inner function.
Comments
Post a Comment