PNG with()- the with statement is intended to provide namespace-style shorthand for hierarchical object access. That is, in the specified code area, the object is called directly by the node name. Here are the examples
1. Common writing method: image.png
2. Write image.png for variable replacement
3. With writing image. PNG
Image.png with(
1. Performance image.png
Simply assign an object attribute 1 million times, and the difference between with is still noticeable: image.png
image.png
2. Unpredictability The unpredictability generated by the with statement is the reason for discarding with. With forces the lexical scope apart, inserting objects temporarily into the scope chain. This makes for tricky code. image.png
The string “sword” is fine with empty objects, but with forces access to A.A when the argument passed is an A object with an attribute of the same name as A
In addition, the variable declared in the with statement does not belong to the with parameter object: image.png
The variable declared in with is actually added to the outer function: image.png
Both cases are caused by the RHS query mechanism triggered by the WITH code
RHS description In the first operation performed by the engine, a query is made on variable A, which is called an LHS query. There are actually two types of engine queries: LHS queries and RHS queries
Literally, an LHS query is performed when a variable appears on the left side of an assignment, and an RHS query is performed when a variable appears on the right
To be more precise, an RHS query is no different from simply looking up the value of a variable, whereas an LHS query is trying to find the container of the variable itself so that it can be assigned the value image.png
Foo (2); This code contains a total of four queries, which are:
1, foo (…). An RHS reference is made to Foo
2, function pass parameter a = 2 LHS reference to a
3, the console. The log (…). The CONSOLE object is RHS referenced and checked to see if it has a log method
4, console.log(a) makes a RHS reference to a and passes the value to console.log(…). Code cannot be optimized because the meaning of the code changes all the time because it cannot be predicted, and different calls, or even the same calls, can be biased by changes in runtime, making the code unable to be optimized.
Optimization refers to two things: parsing and running slowly, which is the performance we talked about earlier. For code optimization and compression tools, on the other hand, you can’t rename a variable or an attribute (because attributes can’t be renamed) if you can’t determine whether it’s a variable or an attribute.