Avoid using JS feature with (obj) {}

Source: Internet
Author: User

1) Brief description
The WITH statement can be conveniently used to refer to an existing property in a particular object, but cannot be used to add properties to an object. To create a new property for an object, you must explicitly refer to the object.

2) syntax format
With (object instance)
{
code block
}
Sometimes, in a program code, I need to use the properties or methods of an object many times, according to the previous wording, all through: Object. property or object. Methods to obtain the properties and methods of the object separately, a bit of a hassle, after learning the With statement, can be implemented in a similar manner as follows:
With (Objinstance)
{
var str = attribute 1;
.....
} removes the hassle of writing object names multiple times.

3) Example

<script language= "JavaScript" > <!--functionLakers () { This. Name = "Kobe Bryant";  This. Age = "28";  This. Gender = "Boy"; }  varpeople=NewLakers ();  with(people) {varstr = "Name:" + name + "<br>"; STR+ = "Age:" + ages + "<br>"; STR+ = "Gender:" +gender;  document.write (str); }  // -</script>

The code performs the following effects:
Name: Kobe Bryant
Age: 28
Sex: Boy

JavaScript provides a with statement intended to use it to quickly access the properties of an object, unfortunately, its results may sometimes be unpredictable, so you should avoid using it

For example

(obj) {  B}

It is equal to one of these statements,

A == = = Obj.a

You can't tell which of these statements you get, and with itself seriously affects the speed of the JavaScript processor, it's meant to be good, but without it, the JavaScript language would be a little better.

Avoid using JS feature with (obj) {}

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.