The reason why the With statement is disabled in strict JavaScript mode is javascriptwith.

Source: Internet
Author: User

The reason why the With statement is disabled in strict JavaScript mode is javascriptwith.

I have read the strict JavaScript mode many times, and some of them say "forbidden With statement". Previously I saw this was riding a horse and watching flowers, because this statement is rarely used at ordinary times, disabling or disabling is not highly related to yourself. Today, I cannot help wondering why the "strict mode" cannot accommodate the with statement?

In EcmaScript specifications, the with statement is used to set the scope of the 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 = 'applicant: '; var zhangsan = new Person ('zhang san', 20, 'mal '); var str = ''; with (zhangsan) {str = title + name + ', age + 'years old,' + sex + ', position '+ job;} console. log (str );})();

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

If you change the with statement block

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

No error is reported. The output str is: Applicant: Zhang San, 20 years old, male, undefined

When executing a variable in the with statement block, check its attributes in zhangsan.

We know that two processes are required to run the Script: Compile and execute.
Obviously, during compilation, it is difficult to determine the attributes of the object represented by this variable in zhangsan. You can only confirm that zhangsan is an instance of Person during execution. Therefore, the variables in the with statement block cannot be the attributes of zhangsan or the variables in the variable scope chain at the time of compilation.

This is different from checking whether the variables are defined when compiling in the strict mode. Therefore, the strict mode does not allow exists. Therefore, it is hard to understand how to disable the With statement in the strict mode.


How can I disable a button on a webpage using javascript? What is the statement? (Correct append score)

Take three check boxes and two buttons as an example:
<Script language = "javascript">
Function checkfun ()
{
Var flag = true;
For (var I = 0; I <(document. forms [0]. elements. length-2); I ++)
If (! Document. forms [0]. elements [I]. checked)
{
Flag = false;
}
If (flag)
{
Document. forms [0]. elements [I]. disabled = "false ";
}
}

Function dreset ()
{
Javascript: window. location. reload ();
}
</Script>

<Body>
<Form name = "form1" method = "post" action = "">
<Input name = "checkbox" type = "checkbox" onClick = "checkfun ()" value = "checkbox">
<Input type = "checkbox" name = "checkbox" value = "checkbox" onClick = "checkfun ()">
<Input type = "checkbox" name = "checkbox" value = "checkbox" onClick = "checkfun ()">
<Input type = "submit" name = "Submit" value = "submit">
<Input type = "submit" name = "Submit" value = "refresh" onClick = "dreset ()">
</Form>
</Body>

Which of the following statements about JavaScript statements is true?

A and B are right and there is no doubt. D is wrong. I also confirm that C and E are not clear.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.