javascript - variables not hoisting as expected in angularjs -


I follow John Pappa and his angular style guideline recommendations. I like the idea of ​​providing all the variables and methods for viewing at the top of the controller and rely on javascript to flip the variable at runtime. I do not think clearly elevation, and at the same time I thought that I did though. In the following example, I expect the $ scope.dumbValue dumbValue variable declaration thrown over the assignment statement;

  var app = angular Module ('plunker', []); App.controller ('MainCtrl', [$ scope, $ http]); Function MainCtrl ($ Range, $ http) {$ scope.dumbValue = dumbValue; Var dumbValue = 'dumb'; }   

But you can see that this is not the case; The selected ng-model value has not been started. If you move an assignment statement under the variable declaration, then the NG-model is initialized.

Why has JavaScript not raised var dumbValue above the assignment, or is it something I can not understand about the angular way it works with it is?

You have to link me where Dad has recommended me to make sure, but I only Seeing that he is talking about the function - scalar is not assuming you are trying to do the latter, and by doing so you get into a problem.

You can see, the moving moves at the peak, but not their definitions (aka assignment ). If you are declaring and defining in a statement, then in the uplift it has been broken into two statements and becomes Hoist in the East.

Then this code:

  var MainCtrl = function ($ scope, $ Http) {$ scope.dumbValue = dumbValue; Var dumbValue = 'dumb'; }   

This code is similar to writing (because the hoisting turns into this):

  var MainCtrl = function ($ scope, $ http) { Var dumbValue; // declared, not defined (hence the value `undefined ') $ scope.dumbValue = dumbValue; // $ dumbValue = 'dumb' for assignment of 'undefined' for a property of scope; // Define your string string value}   

As I said, though, Papa is talking about the tasks that are meant for $ scope Instead of doing so, they say:

  var MainCtrl = function ($ scope, $ http) {function foo () {}; $ Scope.foo = foo; }   

Do this:

  var MainCtrl = function ($ scope, $ http) {$ scope.foo = foo; Function foo () {}; }   

This works because the Hoisting Convert Function designates the statements in the form of variable declarations in the form of variable declarations, then divides them into two separate statements And puts them on top, then the recommended version of Papa (the previous code block) is changed by hosting:

  var MainCtrl = function ($ scope, $ http) {var foo; Foo = function () {}; $ Scope.foo = foo; }   

This different treatment of the named function statement, as you can see, before complying with the code foo Is defined as. Scope is .

Personally, I do not like the recommendation of Papa. Though I understand to understand personally and nothing gets through it, I have seen many problems by writing a code to the developers who got converted into hoarding. is. This is the case, I am writing specially to write my code exactly, because with the majority of my developers it will be changed as a hoop to avoid the code being misunderstood. In this way, the default settings will also write to you.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -