Home >Web Front-end >JS Tutorial >How to use the with statement in js? Usage of with statement in js (code)
The content of this article is about how to use the with statement in js? The usage (code) of the with statement in js has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The scope of the with statement sets the scope of the code to a specific object. The main purpose is to simplify the work of writing the same object multiple times.
var obj = { "name": "hi", "age": 18, "sex": "boy" }; // var name1 = obj.name; // var age1 = obj.age; // var sex1 = obj.sex; // // console.log(name1, age1, sex1); //hi 18 boy with (obj) { var name2 = name; var age2 = age; var sex2 = sex; } console.log(name2, age2, sex2); //hi 18 boy
Special reminder: The with statement is not allowed in strict mode, otherwise it will be regarded as a syntax error.
Related recommendations:
How to use the with statement in js_javascript skills
Use the "with" statement in js Variable reference problem across frames_javascript skills
The above is the detailed content of How to use the with statement in js? Usage of with statement in js (code). For more information, please follow other related articles on the PHP Chinese website!