What is the usage of with in javascript

coldplay.xixi
Release: 2023-01-04 09:35:51
Original
3362 people have browsed it

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.

What is the usage of with in javascript

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 };
Copy after login

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;
Copy after login

And There will be a simple shortcut by using with writing method

with (obj) { a = 3; b = 4; c = 5; }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!