JavaScript Technology Lectures-object-based JavaScript language

Source: Internet
Author: User
Tags date format array cos key words reference variable advantage
Object

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-text files.

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:

referencing JavaScript internal objects;

Provided by the browser environment;

Creates 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

The function of the statement is a control loop that is used to manipulate all the 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.

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), his key words

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), EW 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.

4, reference to object properties

The reference to an object property can be implemented in one of the following three ways:

(1) using the dot (.) operator

University. Name= "Yunnan Province"

University.city= "Kunming"

University. Date= "1999"

Where university is an existing object, Name, city, date is its three attributes, and is assigned a value by operation.

(2) To implement the reference through the subscript of the object

university[0]= "Yunnan"

University= "Kunming"

University= "1999"

Through an array-type Access property, you can use a looping operation to get its value.

function Showunievsity (object)

For (Var j=0;j<2; j + +)

document.write (Object[j])

If the use of for...in can not know the number of its attributes can be achieved:

Function Showmy (object)

For (Var prop in this)

Docament.write (This[prop]);

(3) Through the form of strings to achieve

university["Name"]= "Yunnan"

university["City"]= "Kunming"

university["Date"]= "1999"

5, reference to the method of the object

The reference to an object method in JavaScript is very simple.

Objectname.methods ()

Actually the methods () =functionname method is essentially a function. If you refer to the Showmy () method in the University object, you can use:

document.write (University.showmy ())

Or: document.write (University)

such as the method for referring to Cos () in the math inner object

The

With (math)

document.write (cos (35));

document.write (cos (80));

It is relatively complicated to refer to without using with:

document.write (Math.Cos (35))

document.write (Math.sin (80))



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.