Home > Web Front-end > JS Tutorial > body text

Why JavaScript strict mode disables the With statement

巴扎黑
Release: 2017-09-19 11:32:48
Original
1662 people have browsed it

I have read JavaScript strict mode many times, and one of them said "disable the With statement". Today I can't help but wonder why "strict mode" cannot tolerate the with statement. If you are also confused, you can take a look.

I have read JavaScript Strict Mode many times, and one of them says "disable the With statement". I used to see this as just watching the flowers, because this statement is rarely used, so I can't help but disable it, which is not very relevant to me. big. Today I can't help but wonder why "strict mode" cannot accommodate the with statement?

The EcmaScript specification says that "the with statement is used to set the scope of code in a specific object." It can be seen that the With statement changes the scope chain.


function Person(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
(function(){
var title = '申请人:';
var zhangsan = new Person('张三',20,'男');
var str = '';
with(zhangsan){
str = title+name+',年龄'+age+'岁,'+sex+'性'+',职位'+job;
}
console.log(str);
})();
Copy after login

The above code will report Uncaught ReferenceError: job is not defined.

If you change the above with statement block to

str = title+zhangsan.name+', age'+zhangsan.age+'years old,'+zhangsan.sex+'sex'+', Position'+zhangsan.job;

will not report an error, and the output str is: Applicant: Zhang San, age 20, male, position undefined

For the variables in the with statement block, in When executing, check whether its attributes are in zhangsan.

We know that when running a script, two processes are required, first compilation and then execution.
Obviously at the time of compilation, it is not possible to determine what attributes the object represented by the variable zhangsan has. It can only be determined at execution time that zhangsan is an instance of Person. Therefore, it is impossible to confirm at compile time whether the variable in the with statement block is an attribute of zhangsan or a variable in the upper-level variable scope chain.

This conflicts with strict mode, which checks whether variables are defined at compile time, so strict mode will not allow differences to exist, so it is not difficult to understand that strict mode disables the With statement.

The above is the detailed content of Why JavaScript strict mode disables the With statement. 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
Popular Tutorials
More>
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!