Are there any cases where the usefulness of a lift is beneficial

I came up with the topic of Ascension , while there were some random discussions with JavaScript developers, and I was told that Ascension is NOT a bug, bug or poorly written functionality, but a powerful tool for developers.

Can anyone explain how JavaScript raising variables and functions is a powerful concept in JavaScript or how useful it is when writing code? (or) is it just an unintentional concept that was accidentally discovered by developers?

+4
source share
1 answer

This is my favorite article on the topic of lifting and other issues / features of the JavaScript area and explains it all better than I could ever hope for:

http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html

EDIT:

I missed what you really asked when the climb is useful, and not just what it does.

So, now that you have read this article and know what it is doing, here is a very general snippet that uses the lift:

JS file:

 var myVar = myVar || {}; myVar.foo = function(){}; //etc... 

You will see that this is used in many OOP JavaScript files to declare an object. The reason we can write it like this is due to the rise in Javascript.

Without a lift, we would have to write

if (typeof myVar! = 'undefined') {var myVar = myVar; } More {var myVar = {}; }

or something like that.

Another neat trick allows you to do the following:

 var a, b = a = "A"; 

This is equivalent to:

 var a = "A"; var b = a; 

Another nice thing about lifting happens when you write functions.

In a more traditional language, without lifting, you need to write all your functions at the top of the page before any code is executed. Otherwise, you will receive an error message that the function does not exist.

With the lift, your functions can be declared anywhere (as long as you use var ), and they will be raised up, which means that you will not get these errors.

Now, if you are writing your own code, then you should declare your variables at the top of your field anyway, but even if you have a couple script files that you are trying to merge into one, Lifting makes this process much easier.

+1
source

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


All Articles