In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the usage of with in javascript function". In daily operation, I believe many people have doubts about the usage of with in javascript function. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "the usage of with in javascript function". Next, please follow the editor to study!
Analysis of the usage of with function in js function
The definition is convenient to refer to an existing property in an object
But it cannot be used to add properties to an object.
The new property must explicitly reference the object * /
Code format
With (object) statements
Object: new default object
Statements: one or more statements oject is the default object for this statement
With statements are often used to shorten the amount of code that must be written in a particular situation.
X = Math.cos (3 * Math.PI) + Math.sin (Math.LN10) y = Math.tan (14 * Math.E) with (Math) {x = cos (3 * PI) + sin (LN10) y = tan (14 * E)} function valiadate_reauired
To put it simply, with is equivalent to a sketch in a specified area of code, and the object can be called through the node name.
Common way of writing:
Var car= {size: suv, color:yellow, money:1500}
Other classes call
Function= {car car = new car (); car.size=suv; car.color=yellow; car.money=1500;}
Use with to write: omit car.
With (car) {size=suv; color=yellow; money=1500;}
The car object is associated with the with statement, and the interior of the with code block treats each variable as a local variable when parsing
If the local variable has the same name as the property of the car object (such as size), the local variable points to the property in car
Disadvantage 1
With automatically creates a global variable in the global scope and throws a ReferenceError exception in strict mode.
Withwith modifies or creates new scopes at run time to deceive other lexical scopes defined at writing time.
Disadvantage 2
With is much slower than not using it under the same conditions, specifically because js is precompiled before it is run, and some optimizations rely on the ability to statically analyze the code according to the morphology of the code and predetermine the definition location of all variables and functions in order to quickly find identifiers during execution.
But if the engine finds with in the code, it can simply assume that the judgment about the location of the identifier is invalid, because there is no way to know what is passed to the object that with uses to create a new lexical scope. So js will not optimize
At this point, the study of "the use of with in the javascript function" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.