In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
If sentence several kinds of optimization is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
In the documentation of UglifyJS, a tool for compressing and beautifying javascript, I saw several ways to optimize if statements.
Although I haven't used it to do some tentative tests, I can see that it does beautify js. Some people may think that the if statement is that simple, to what extent can it be optimized? But take a look at the following ways, you may change your mind.
First, use common ternary operators
If (foo) bar (); else baz (); = > foo?bar (): baz (); if (! foo) bar (); else baz (); = > foo?baz (): bar (); if (foo) return bar (); else return baz (); = > return foo?bar (): baz ()
You are certainly no stranger to using ternary operators to optimize if statements above, or you use it often.
Use the and (& &) and or (| |) operators
If (foo) bar (); = > foo&&bar (); if (! foo) bar (); = = > foo | | bar ()
To be honest, I didn't write code like this. I saw it when I was learning "Brother Bird's Linux Cuisine", but I didn't think of implementing it in js.
3. Omit curly braces {}
If (foo) return bar (); else something (); = > {if (foo) return bar (); something ()}
You and I are both familiar with this way of writing, but I suggest doing it when the code is optimized, or leave it to UglifyJS to help you solve it. After all, without a brace, the code is not readable.
At this point, I think of a way for the father of jQuery to get attributes of HTML elements in "mastering JavaScript."
Function getAttr (el, attrName) {var attr = {'for':'htmlFor',' class':'className'} [attrName] | | attrName;}
If we don't write this way, we may need to deal with it with the help of two if statements, and the above code is not only concise and efficient, but also readable.
If you think about it, we can find an effective way to solve the problem for many times, but the key is whether we try our best to find a better way.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.