In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "yield sample Analysis of ES6". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "yield sample Analysis of ES6".
Recently, the development of ES6 standard is in full swing, mainly with the support of major browsers, the most important manufacturer Alipay and Wechat, which makes the domestic development very fast. This is mainly about the yield keyword. Yield can actually be regarded as a new interrupt mechanism. Everyone knows that the execution of javascript functions is sequential, there is no pause, and waiting for messages can only be completed through delay checks such as callbacks or settimeout.
Yield example Analysis of ES6
Some examples say that yield is related to threads, but I don't see it in the official example. All right, let's get to the topic and take a look at the official example first.
Function*foo (x) {
Vary=2* (yield (Xero1))
Varz=yield (ybin3)
Return (x+y+z)
}
Varit=foo (5)
Console.log (it.next ())
Console.log (it.next (12))
Console.log (it.next (13))
/ / the returned values are {value:6,done:false}, {value:8,done:false}, {value:42,done:true}
Main points of yield example
The first point: foo needs to be * type, the new * brings this function pointer, this function must be * type, otherwise varit=foo (5) means to get the result of function execution, and pointer type means that it gets the function.
The second point: the first it.next () indicates that the function execution starts until the first yield stops. The return value is yield (the value of the expression), and the return value at this time is 5, and the result is 6. 5. If the it.next here takes parameters, the parameters will be ignored because there are no parameters to pass in the first execution.
The third point: the second it.next (12) indicates that the return result of the previous yield is 12. If you take it into the vary=2*12;, you must have a value, because there is an expression in the yield, which is not a constant and cannot be calculated by yourself. Why not take the previous x default into the calculation here? it can only be said that this is not the current mechanism.
It should take into account the implementation of the complex program, because x here is a variable, in actual operation, this may also be an expression, and even the scope of the problem to consider, the calculation here is the next yield (24amp 3), the result is 8.
Fourth point: the third it.next (13) also indicates that the return result of the previous yield is 13, that is, there is no yield after the varz=13;, so go to the function return value return5+24+13, the result is equal to 42. At this time, the value of done is true, indicating the end of the function.
The important summary: the function needs to be a pointer, and remember that the result of the yield (expression) execution is the value of the previous execution.
So is the keyword of yield clear?
In practical work, it.next () does not have to go to the final return of the function, such as executing it.next () for the first time to get a value that does not conform to the expected return value, so you can directly not perform later next, such as multi-layer check.
Thank you for your reading, the above is the content of "ES6 yield sample Analysis". After the study of this article, I believe you have a deeper understanding of ES6 yield sample analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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: 251
*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.