Performing native functions with AngularJS

What are the benefits of using native functions with a framework, such as Angular?

I am new to Angular, but my understanding so far is module-based design, giving me most of the benefits that the self-executing function provides. What am I missing? Is this just a matter of style?

Here is an example of Ben Nadel. I really like the style, but I want to understand if there are any benefits by writing Angular code this way, or if it is basically a style choice.

+7
source share
3 answers

Basically, it ensures that your code will not be declared in the global scope, and that any variables you declare remain within your function.

In this case, it is also useful to declare the objects needed to run the code in one place. You can clearly see below that the angular and Demo objects are passed, and nothing more. If the code was not wrapped in a function, you will have to scan the code to see what the dependencies were.

Personally, I prefer to use a module loader, such as RequireJS , which forces you to follow this pattern.

+7
source

Such a question of opinion. The main advantage that I see in performing my own functions is not to create global variables. I have never seen this template with angular.

For the example of the link you gave, it has no advantage. The angular variable will exist anyway in the angular application, so you can use angular directly. And the daemon, which is a module, you can add controllers to it without entering the global scope.

I like a lot of functions performed independently. But in this case, I really do not see the benefits.

+2
source

Daniel, you said, "If it's basically a style choice . " I know at least two examples in javascript when the “code style” is not only a matter of preference, but also a different result.

Are semicolons optional? It's my pleasure.

 $scope.test = function() { console.log('Weird behaviour!') } //; let us comment it (function() {} ()); //two functions seem to be independent 

equally

 $scope.test = function() { console.log('Weird behaviour!') }(function() {} ()); //but without semicolon they become the one 

Another example of a "code style" that is not related to self-starting functions:

 var x = (function() { return //returns undefined {}; }()); alert(x); /* that is why, I suppose, while writing javascript code, we put function brackets in the following "code style": function() { //at the same line return { //at the same line, this style will not lose the object }; } */ 

Code style formation is dictated by unexpected results of this kind.

Last but not least. With a self-closing function: a closure is created when the function is called and keeps your variables local.

A closure is created when a function is called. That is why the self-reproducing function is so convenient. As Daniel correctly noted, this is a good place to store an independent code unit, this template is called a modular template . Therefore, when you switch from pure javascript to a specific environment or vice versa, this independence makes code changes smoother. The best option is to simply move your module to the corner wrapper and reuse it.

So it’s convenient for transferring code from one technology to another. But, I believe, this does not make sense for a specific framework.

+2
source

Source: https://habr.com/ru/post/953666/


All Articles