Home  >  Article  >  Web Front-end  >  How to use the with statement in js? Usage of with statement in js (code)

How to use the with statement in js? Usage of with statement in js (code)

不言
不言Original
2018-08-15 15:58:502390browse

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!

Statement:
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