Deep understanding of objects in JavaScript

Source: Internet
Author: User

This article mainly introduces the in-depth understanding of the objects in JavaScript, is the basic knowledge of JS introductory learning, need friends can refer to the

JavaScript is an object-oriented programming (OOP) language. A programming language can be referred to as object-oriented, providing developers with four basic functions:

Encapsulation-stores related information, whether data or methods, or objects

Aggregation-Stores an object to the inside of another object

Inheritance-the ability of a class depends on another class (or number of classes), and the properties and methods used for its part

Polymorphism-writing functions or methods to work in a variety of different ways

Object is by property. If the property contains a function, it is considered a method of an object, otherwise, the property is considered to be a property.

Object properties:

An object's properties can be any of the three basic data types, or any abstract data type, such as another object. An object property is usually a variable of the method of an object used internally, but it can also be a variable for the entire page to be globally visible.

The purpose syntax for adding attributes is:

?

1 Objectname.objectproperty = PropertyValue;

Example:

The following is a simple example of how to use the properties of the "title" file object to get the document title:

?

1 var str = document.title;

Method of the object:

The method is to let the object do something. The difference between a function and a method is that a function statement of a separate unit and method is appended to the object, and can be referenced by this keyword is not much different.

Method can be useful for everything from the on-screen contents of a display object to performing complex mathematical operations on a set of local properties and parameters.

Example:

Here is a simple example of how to use the method of a write () Document object to write anything in the document:

?

1 document.write ("This is Test");

User-defined objects:

All user-defined objects and built-in objects are descendants of objects that are called objects.

New operator:

The new operator is used to create an instance of an object. To create an object, the new operator is followed by the constructor method.

In the following example, the constructor method object (), Array (), and Date (). These constructors are built-in JavaScript functions.

?

1 2 3 var employee = new Object (); var books = new Array ("C + +", "Perl", "Java"); var day = new Date ("August 15, 1947");

Object () constructor:

Constructors are functions that are used to create and initialize objects. JavaScript provides a special constructor that calls object () to build objects. The return value of the Object () construct is assigned to a variable.

The variable contains a reference to the new object. The attribute assigned to the object is invariant and is not defined using the var keyword.

Example 1:

This example demonstrates how to create an object:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16

Example 2:

This example shows how to create an object, a user-defined function. Here this keyword is used to refer to an object that has been passed to a function:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18

The object that defines the method:

The previous example shows how to construct a function to create an object and assign a property. However, we need to allocate the method to complete the definition of an object.

Example:

The following is a simple example that shows how to add a function to an object:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The

with keyword:

The WITH keyword as a shorthand property or method of a Reference object.

An object that is specified as a parameter is the default object for the duration of the next block. Properties and methods for an object can be in an object that is not named.

Grammar

?

1 2 3 With (object) {properties used without the object name and Dot}

Example:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The

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.