In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the javascript conditional access properties and arrow function example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian with you to understand.
Conditional access attributes
?. Is a new feature introduced by ES2020, is a conditional property access operator, when you access a property value of the undefined variable, if you use. The operator reports an error directly, and returns undefined if accessed using the conditional property access operator.
Look at the example:
Let book = {price:10, edition:10, name: "javascirpt"} console.log (book.page.num)
Report an error directly:
TypeError: Cannot read property 'num' of undefined
Because the value of book.page undefined, undefined does not have any attribute value, it will report an error.
If you are not sure whether a value is a undefined or an object, in addition to using the if statement to determine, you can also use the conditional access operator to access a property directly, even if the object being accessed is undefined, it will not report an error. Instead, return undefined.
Console.log (book.page?.num)
Output:
Undefined
2. Introduction of Arrow function
The arrow function is an abbreviated way to define a function in ES6, separating the parameter list and the function body with = >.
Example:
Let square = x = > square (3)
Output:
nine
The definition of this function is equivalent to a traditional function:
Function square (x) {return Xerox 2}
The arrow function is usually used to pass an unnamed function as an argument to another function.
Let nums = [1Jing 2Jing 3JI 4] .map (x = > Xerox 2) console.log (nums)
Output:
[2, 4, 6, 8]
The arrow function makes the code look more concise.
If you use the traditional function keyword to define a function, it looks a bit verbose.
Nums = [1Jing 2 Jing 3 Jing 4] .map (function (x) {return Xue2}) console.log (nums)
If the arrow function has multiple arguments, it needs to be enclosed in parentheses
Const add = (xprimey) = > xtropy _ console.log (add (1pr _ 2))
If the function body of the arrow function has more than one statement, enclose the function body in large parentheses and return the value with the return keyword
Const add = (XJING y) = > {let tmp=x+y;return tmp}; console.log (add (1Pol 2))
At this point, the function body of the arrow function is exactly the same as the normal function body defined by function. Therefore, the arrow function is concise and readable in a simple one-line statement. Once the function body is too complex, it is not so good to use the arrow function to define readability.
Thank you for reading this article carefully. I hope the article "sample Analysis of javascript conditional access Properties and Arrow functions" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.