JavaScript Getting Started tutorial (a) JS object-oriented programming _ Basics

Source: Internet
Author: User
Tags cos sin
The WITH statement specifies the default object for one or a set of statements.
Usage:
With (< OBJECT >) < statement >;
The With statement is typically used to shorten the amount of code that must be written in a particular case. In the following example, note the repeated use of Math:
x = Math.Cos (3 * Math.PI) + Math.sin (MATH.LN10);
y = Math.tan (MATH.E);
When you use the WITH statement, the code becomes shorter and easier to read:
Copy Code code as follows:

With (Math) {
x = cos (3 * PI) + sin (LN10);
y = tan (E);
}

The This object returns the current object. In different places, this represents a different object. If this is used in JavaScript in the main program (not in any function, not in any event handler), it represents the Window object, and if this is used in a with statement block, it represents the object specified with, if it is in an event handler Using this, it represents the object where the event occurred.
A commonly used this usage:
Copy Code code as follows:

<script>
...
function Check (formobj) {
...
}
...
</script>
<body ...>
...
<form ...>
...
<input type= "text" ... onchange= "check (this.form)" >
...
</form>
...
</body>

This usage is often used to immediately detect the validity of form input.
Custom constructors We already know that a constructor such as Array (), Image () allows us to construct a variable. In fact, we can write our own constructor. Custom constructors are also used with function. Use this to define the attribute inside the function.
Copy Code code as follows:

Function < constructor function name > [< parameter >]] {
...
this.< Property name > = < initial value >;
...
}

Then, use the new constructor keyword to construct the variable:
var < variable name > = new < constructor function name >[(< parameter >)];
After constructing the variable,< variable name > becomes an object, it has its own attributes--the properties set in this function.
Here's an example of a custom constructor that collects browser details from the Web:
Copy Code code as follows:

The function is () {
var agent = Navigator.userAgent.toLowerCase ();
This.major = parseint (navigator.appversion); Major Version number
This.minor = parsefloat (navigator.appversion);//Full version number
This.ns = ((Agent.indexof (' Mozilla ')!=-1) &&
((Agent.indexof (' Spoofer ') ==-1) &&/whether Netscape
(Agent.indexof (' compatible ') = = 1));
This.ns2 = (This.ns && (this.major = 3)); is Netscape 2
THIS.NS3 = (This.ns && (this.major = 3)); is Netscape 3
this.ns4b = (This.ns && (This.minor < 4.04)); is Netscape 4 low version
THIS.NS4 = (This.ns && (this.major >= 4)); is Netscape 4 high version
this.ie = (Agent.indexof ("MSIE")!=-1); Whether IE
This.ie3 = (this.ie && (this.major = 2)); Whether IE 3
This.ie4 = (this.ie && (this.major >= 4)); Whether IE 4
THIS.OP3 = (Agent.indexof ("opera")!=-1); is Opera 3
This.win = (Agent.indexof ("Win")!=-1); Whether the Windows version
This.mac = (Agent.indexof ("Mac")!=-1); is Macintosh version
This.unix = (Agent.indexof ("x11")!=-1); is Unix version
}
The var is = new is ();

This constructor is a complete collection of browser information. We see that it defines a number of attributes for an object: Major, minor, NS, ie, win, Mac, and so on. They are meant to see the notes above. After the is variable is defined as an IS () object, it is convenient to know the browser's information in the format of if (IS.NS). We can also see from this constructor that it can also use a generic JavaScript statement (the VAR statement in the example above).
Let's take another look at a constructor that uses parameters:
Copy Code code as follows:

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 ', ', ' Dec, 1982 ', ' Student ');

From this constructor we see not only the use of parameters, you can also see that different properties are possible with different data types (the example five attributes are string, Boolean, number, date, string), and you can also see the constructor in the constructor to "construct" the property. If enough "protection" is used to avoid infinite loops, you can construct your own properties with the constructor itself.
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.