Objects in JavaScript

Source: Internet
Author: User
Tags advantage

The JavaScript language is object-based (object-based) rather than object-oriented (object-oriented). It is said to be an object-based language, mainly because it does not provide many features such as abstraction, inheritance, overloading, and so on object-oriented languages. Instead, the complex objects created by other languages are unified to form a very powerful object system.

Although the JavaScript language is an object-based object, it still has some basic object-oriented features. It can create its own objects as needed to further expand the scope of JavaScript applications and to enhance the writing of powerful Web documents.

The basics of an object

1, the basic structure of the object

Objects in JavaScript are composed of two basic elements of attributes (properties) and methods (methods). The former is an object that implements the loading unit of the information in the process of carrying out its required behavior, which is associated with the variable, which means that the object can be executed according to the designer's intention, which is associated with a particular function.

2, the way to refer to the object

To be truly used, an object can be obtained in the following ways:

o referencing JavaScript internal objects;

O is provided in the browser environment;

o Create a new object.

This means that an object must exist before it is referenced, otherwise the reference will be meaningless and an error message appears. From the above we can see that JavaScript reference objects can be obtained in three ways. Either create a new object or take advantage of an existing object.

3. Related object Operation statement

JavaScript is not a pure object-oriented language, it has many features to provide object-oriented language, so the JavaScript designer makes it you "object-based" rather than object-oriented language, Several statements and keywords and operators are provided in JavaScript for manipulating objects.

(1) for...in statement

The format is as follows:

For (object property name in known object name)

Description

o The function of the statement is a control loop that is used to manipulate all properties of a known object. Instead of using a counter, it is a repeated reset of all the attributes of a known object to a variable.

o The advantage of this statement is that you do not need to know the number of attributes in the object to operate.

Example: The following function is the display of the contents of an array:

Function showData(object)
for (var X=0; X<30;X++)
document.write(object[i]);

This function accesses the properties of each object by using an array subscript order value, in which case you must first know the subscript value of the array, or an error occurs if it is out of range. To make a for...in statement, you don't need to know the number of object attributes at all, see below:

Function showData(object)
for(var prop in object)
document.write(object[prop]);

When you use this function, in the loop body, the for automatically takes out the property until you finally do so.

(2) With statement

Using this statement means that any reference to a variable in the statement body is considered to be an attribute of the object to save some code.

with object{
...}

All the statements in the curly braces that follow the WITH statement are scoped to the object at the end.

(3) This keyword

This is a reference to the current in JavaScript because the reference of an object is multi-level, multidimensional, often a reference to one object requires a reference to another object, and another object may have to refer to another object, which may cause confusion, and finally you do not know the object of the reference now, This javascript provides a statement that specifies the current object for the object.

(4) New operator

Although the functions of objects in JavaScript are already very powerful. But what's more powerful is that designers can create their own objects as needed to meet a specific requirement. Use the new operator to create a new object. Its creation object uses the following format:

Newobject=new Object (Parameters table);

Where NewObject creates a new object: Object is an existing, Parameters table parameter list, and new is a command statement in JavaScript.

such as creating a date new object

Newdata=new Data ()

Birthday=new Data (December 12.1998)

You can then make NewData, birthday as a new Date object.

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.