Usage of with in javascript: 1. The with statement provides a namespace-style shorthand method for level-by-level object access; 2. A shortcut to repeatedly reference multiple properties in the same object does not need to be repeated. Reference to the object itself.
The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.
Usage of with in javascript:
The original intention of the with statement is to provide a namespace-style shortcut for level-by-level object access. That is, in the specified code area, call the object directly by the node name.
with is usually used as a shortcut to repeatedly reference multiple properties in the same object without repeatedly referencing the object itself.
For example, there is currently an object like this:
var obj = { a: 1, b: 2, c: 3 };
If you want to change the value of each item in obj, the general writing method may be like this:
// 重复写了3次的“obj” obj.a = 2; obj.b = 3; obj.c = 4;
And There will be a simple shortcut by using with writing method
with (obj) { a = 3; b = 4; c = 5; }
In this code, the with statement is used to associate the obj object, which means that inside the with code block, each variable is first Considered as a local variable, if the local variable has the same name as a property of the obj object, then this local variable will point to the property of the obj object.
Related free learning recommendations:javascript video tutorial
The above is the detailed content of What is the usage of with in javascript. For more information, please follow other related articles on the PHP Chinese website!