Object-Oriented Programming in JavaScript

Source: Internet
Author: User

Statements about object-oriented programming now have the strength to learn the following about object-oriented programming, but they actually belong to the content of the previous chapter.

The with statement specifies the default object for one or more statements.

Usage:
With (<Object>) <Statement>;

The with statement is usually used to shorten the amount of code that must be written in a specific situation. In the following example, pay attention to the reuse of Math:
X = Math. cos (3 * Math. PI) + Math. sin (Math. LN10 );
Y = Math. tan (14 * Math. E );

When the with statement is used, the Code becomes shorter and easier to read:
With (Math ){
X = cos (3 * PI) + sin (LN10 );
Y = tan (14 * E );
}

This object returns the "current" object. In different places, this indicates different objects. If this is used in the JavaScript "main program" (not in any function, not in any event handler), it represents the window object; if this is used in the with statement block, it indicates the object specified by with. If this is used in the event handler, it indicates the object where an event occurs.

A common usage of this:
<Script>
...
Function check (formObj ){
...
}
...
</Script>

<Body...>
...
<Form...>
...
<Input type = "text"... onchange = "check (this. form)">
...
</Form>
...
</Body>

This method is often used to immediately check the validity of form input.

We already know the User-Defined constructor. The constructor such as Array () and Image () allows us to construct a variable. In fact, we can also write our own constructor. User-Defined constructors also use functions. This is used in the function to define attributes.
Function <constructor Name> [(<parameter>)] {
...
This. <attribute name >=< initial value>;
...
}

Then, use the new constructor keyword to construct the variable:
Var <variable name> = new <constructor Name> [(<parameter>)];

After a variable is constructed, <variable name> becomes an object, which has its own attributes-attributes set in the function using this.

The following is an example of a custom constructor that collects browser details from the Internet:
Function Is (){
Var agent = navigator. userAgent. toLowerCase ();
This. major = parseInt (navigator. appVersion); // main version
This. minor = parseFloat (navigator. appVersion); // full version
This. ns = (agent. indexOf ('mozilla ')! =-1 )&&
(Agent. indexOf ('spoofer ') =-1) & // whether it is Netscape
(Agent. indexOf ('compuble ') =-1 )));
This. ns= (this. ns & (this. major = 3); // whether Netscape 2
This. ns3 = (this. ns & (this. major = 3); // whether Netscape 3
This. ns4b = (this. ns & (this. minor <4.04); // check if the version is earlier than Netscape 4.
This. ns4 = (this. ns & (this. major> = 4); // whether it is a high version of Netscape 4
This. ie = (agent. indexOf ("msie ")! =-1); // whether it is IE
This. ie3 = (this. ie & (this. major = 2); // whether it is IE 3
This. ie4 = (this. ie & (this. major> = 4); // whether it is IE 4
This. op3 = (agent. indexOf ("opera ")! =-1); // whether it is Opera 3
This. win = (agent. indexOf ("win ")! =-1); // whether the Windows version is used
This. mac = (agent. indexOf ("mac ")! =-1); // indicates whether the Macintosh version is used.
This. unix = (agent. indexOf ("x11 ")! =-1); // whether the Unix version is used
}

Var is = new Is ();

This constructor collects browser information very completely. We can see that it defines many attributes for the object: major, minor, ns, ie, win, mac, and so on. Their meanings can be found in the preceding annotations. After defining the is variable as the Is () object, you can easily know the browser information in the format of if (is. ns. We can also see from this constructor that it can also use general JavaScript statements (var statements in the above example ).

Let's look at a constructor using parameters:
Function myFriend (theName, gender, theAge, birthOn, theJob ){
This. name = theName;
This. isMale = (gender. toLowerCase = 'male ');
This. age = theAge;
This. birthday = new Date (birthOn );
This. job = theJob
}

Var Stephen = new myFriend ('Stephen ', 'male', 18, 'dec 22,198 2', 'student ');

From this constructor, we can not only see the usage of parameters, but also see that different attributes can be used with different data types (in the preceding example, the five attributes are: String, Boolean, number, date, string), you can also see the constructor can also be used to "construct" attributes. If enough "protection measures" are used to avoid infinite loops, you can also use constructors to construct your own attributes.

Related Article

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.