JavaScript in AJAX supports object-oriented basics-Javascript tutorial

Source: Internet
Author: User
JavaScript: JavaScript in AJAX supports the basics of object-oriented. In the idea of object-oriented Javascript, one of the core concepts is class. A class represents the abstraction of a class of things with similar properties. By instantiating a class, you can obtain an instance belonging to the class, that is, an object.

The method for defining a class in JavaScript is as follows:

Function class1 (){
// Class member definition and constructor
}

Class1 is both a function and a class. It can be understood as a class constructor and is responsible for initialization.

   Use the new operator to obtain an instance of a class

The new operator has been used to introduce basic objects. For example:

New Date ();

Create a Date object, and Date indicates the Date class, but this class is provided internally by JavaScript rather than user-defined.

The new operator is not only effective for internal classes, but also for user-defined classes. for class1 defined in the previous section, you can use new to obtain an instance:

Function class1 (){
// Class member definition and constructor
}
Var obj1 = new class1 ();

Aside from the concept of classes, class1 is a function in the code form. Can all functions be operated using new? Yes, in JavaScript, functions and classes are a concept. When a new operation is performed on a function, an object is returned. If no class member is initialized in this function, an empty object is returned. For example:

// Define a hello Function
Function hello (){
Alert ("hello ");
}
// Obtain an object through a new function
Var obj = new hello ();
Alert (typeof (obj ));

According to the running results, the hello function is executed, and obj also obtains an object reference. When a new function is used, this function is the constructor of the class. The code in this function is considered to initialize an object. Functions used to represent classes are also called constructors.

  Use square brackets ([]) to reference object attributes and Methods

In JavaScript, each object can be considered as a collection of multiple attributes (methods). It is very easy to reference an attribute (method), such:

Object Name. Attribute (method) Name

It can also be referenced in square brackets:

Object Name ["attribute (method) Name"]

Note that the method name and attribute name here are a string, not the original vertex (? ), For example:

Var arr = new Array ();
// Add an element to the array
Arr ["push"] ("abc ");
// Obtain the length of the array
Var len = arr ["length"];
// Length of the output Array
Alert (len );



The execution result is displayed.

The above code is equivalent:

Var arr = new Array ();
// Add an element to the array
Arr. push ("abc ");
// Obtain the length of the array
Var len = arr. length;
// Length of the output Array
Alert (len );

This method of referencing attributes (methods) is similar to arrays, reflecting the property that JavaScript objects are a set of attributes (methods.

[1] [2] [3] [4] [5] Next page

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.