New Function way to declare functions normally should not be used. When the callback function has one short statement, the arrow function is a good option too, because it creates short and light code.įor a shorter syntax when declaring methods on object literals, the shorthand method declaration is preferable. If the function uses this from the enclosing function, the arrow function is a good solution. There are some rules however that you may follow in common situations. The decision of which declaration type to choose depends on the situation. Because the function body is evaluated on runtime, this approach inherits many eval() usage problems: security risks, harder debugging, no way to apply engine optimizations, no editor auto-complete. Remember that functions seldom should be declared using new Function(). Let's see how this is accessed from the outer function: This is nice because you don't have to use myFunc.bind(this) or store the context const self = this when a function needs the enclosing context. This keyword is a confusing aspect of JavaScript (check this article for a detailed explanation of this).īecause functions create their execution context, often it is difficult to detect this value.ĮCMAScript 2015 improves this usage by introducing the arrow function, which takes the context lexically (or simply uses this from the immediate outer scope). You are free to use rest parameters (.params), though. arguments object is not available in the arrow function (contrary to other declaration types that provide arguments object). However, the engine can infer its name from the variable holding the function The arrow function does not create its execution context but takes it lexically (contrary to function expression or function declaration, which creates its own this depending on invocation).The function declared using a fat arrow has the following properties: The following example uses a shorthand method definition in an object literal:ĪbsValue is an arrow function that calculates the absolute value of a number. const factorial = function(n) that delimits the body statements. It is possible to use a function expression and assign it to a regular variable, e.g. If you put a line break where there shouldn't be one, ASI may jump in and assume a semicolon even if there shouldn't be one.Ĭompanies and open source projects will likely have a preference one way or another, so be sure to note what it is.įinally, if you think you may be running into errors due to ASI, check out Babeljs.io to debug - it allows you to put in your code and shows you where ASI semicolons are being inserted.Inside factorial() a recursive call is made using the variable that holds the function: factorial(n - 1). For example, compression or minification could cause your valid code to throw an error because those programs may rely on semicolons.Īlso, it can be harder to debug without semicolons since your code may be concatenating together without you realizing it. If you're going to write your JavaScript without optional semicolons, it's probably good to at least know what ASI is doing. Note: You do need one after: do while (.) Here are a few cases where you don't need semicolons: So, if parsing a new line of code right after the previous line of code still results in valid JavaScript, ASI will not be triggered. Here are 3 main points to be aware of when it comes to ASI.ġ- A semicolon will be inserted when it comes across a line terminator or a '}' that is not grammatically correct. Below I highlight the main takeaways from these resources. I also found a blog post from Bradley Braithwaite on the topic. I found a helpful lecture from Fullstack Academy on the topic, which you can check out here. ASI doesn’t mean that actual semicolons are inserted into your code, it’s more of a set of rules used by JavaScript that will determine whether or not a semicolon will be interpreted in certain spots. The reason semicolons are sometimes optional in JavaScript is because of automatic semicolon insertion, or ASI. I decided to look more into semicolon usage in JavaScript to truly understand why we would or would not want to use them, and use this knowledge to avoid creating any bad habits early on. During one of our first JavaScript lectures at the Flatiron School, the instructor mentioned that semicolons are optional in JavaScript… except when they’re not □
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |