The ECMAScript statement with statement

With the ECMAScript statement for setting a code for a particular object in a scope (with slow, slower set attribute values ​​best avoided with statement)

A, with statements for the character string (complex toUpperCase () method)

var a = "CGLweb distal";
with (A) {
the console.log (the toUpperCase ()); // Output "CGLweb distal"
}

Two, with statement can easily be used to refer to a particular object in the existing property, but can not be used to add properties to the object. To give an object to create a new property, you must explicitly refer to the object

function xinxi() {
this.name = "青格勒";
this.age = "28";
this.gender = "男";
}
var people=new xinxi();
with(people)
{
var str = "姓名: " + name;
str += "、年龄:" + age;
str += "、性别:" + gender;
console.log(str);
}

Three, with the statement of the object is not added as an execution environment to the scope, but in the implementation of environmental effects

var obj1 = [
{a: 11},
{c: 12}
];
function cgl() {(www.gendna5.com)
var a = 2;
with (obj1) {
{a = 3};
{c = 4};
}
console.log(a); //3
console.log(c); //4
console.log(obj1); //[ { a: 11 }, { c: 12 } ]
console.log(obj1[0].a); //11
console.log(obj1[1].c); //12
}
cgl();
console.log(obj1[0].a); //11
console.log(obj1[1].c); //12

Because of this limited information, and he said here.

Guess you like

Origin blog.51cto.com/14513127/2438364