简体   繁体   中英

Arrow function assigned to variable without variable declaration keyword?

I'm not very well-acquainted with arrow functions and encountered a usage of them I don't understand. After looking through a few articles on arrow functions, it seems like a variable declaration keyword should precede the variable name. If not, why wouldn't that be the case? (The code works.)

For example why isn't var , let , or const before getParameterByName ?

getParameterByName = (name, url) => {
 ...
}

In sloppy-mode JavaScript, assignment to a variable that isn't declared will set that property on the global object. This (as you can tell because this question exists) is confusing behaviour.

In general you should always use strict mode . Many confusing behaviours are fixed in strict mode, including the case you ask about here.

 'use strict'; a = () => {}; 

As you can see, running this snippet gives a ReferenceError because a is not declared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM