Object-oriented JavaScript programming and its Scope processing

Source: Internet
Author: User
Tags array object empty function definition functions inheritance return

This article first explained the JavaScript mechanism, and combined with the current popular open source JavaScript framework to explain how to implement object-oriented and inheritance mechanism in JavaScript; This article will cause misunderstanding and Scope in object-oriented JavaScript programming , and finally, some suggestions are given on the Scope of object-oriented JavaScript programming.

object-oriented and simulated inheritance
JavaScript is a script language for weak type interpretation operations, which is not an object-oriented language in terms of language itself. But we can use some of the language features to simulate object-oriented programming and inheritance mechanisms, all of which need to start with the function in JavaScript.
A Function is a snippet of JavaScript code that is defined once but can be invoked or executed countless times. A Function can have 0 or more input parameters and a return value, which is usually used to perform tasks such as calculation or transaction processing. Usually we define a function like this:
Function Definition Example
function distance (x1, y1, x2, y2) {
var dx = x2-x1;
var dy = y2-y1;
Return math.sqrt (DX*DX + dy*dy);
}

In JavaScript, a function is not only a grammatical structure, it can also be used as data. This means that it can be assigned to variables, the attributes of the elements in an object or array are stored, or passed as function arguments, and so on. For example:
function as a data sample
var d = distance;
D (1,1,2,2);
When a function is defined and invoked in an object, this function is called a method of the object. It is important to note that when this function is invoked, the object is passed into the function in the form of an implied parameter, which can be used to refer to the object's properties through the This keyword. For example, the following example runs the result calculator.result with a value of 2.
This keyword example
var calculator = {
Operand1:1,
Operand2:1,
Compute:function () {
this. result = that. Operand1 + this. Operand2;
}
};
Calculator.compute ();
alert (Calculator.result);
In JavaScript, the creation of objects is usually done through the new operator. The new keyword must be followed by an executable function. For example:
New Keyword Example
var array = new Array (10);
var today = new Date ();
When the above creation statement is executed, an empty object is first assigned to the previous variable, then the following function is invoked, and the empty object is passed into the function as a suppressed parameter. This allows the object to be referenced by the This keyword inside the function, doing some object initialization work. Such a function is often referred to as a construction method or a simple construction method. For example:
Construction Method Sample
function Rectangle (W, h) {
this. width = w;
this. Height = h;
}
var rect1 = new Rectangle (2, 4);
Now that we've got objects, functions, functions as data, object methods, and constructors, we can use these JavaScript features to simulate writing Java-style object-oriented code. For example:

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.