August 4 = 341-345

Source: Internet
Author: User

13.10 objects used

13.10.1 object-oriented concept

Javascript is not an object-oriented programming language. The basic features of object-oriented design include inheritance,

Polymorphism is not well implemented. For example, in Java, all classes can be displayed through extends

Inherit the parent class, or inherit the system's object class by default. While JavaScript does not provide standard syntax

Developer-defined classes.

For example, you can use the following code to create a person instance in Java:

Person P = new person ();

Every function in javascript can be used to create objects. The returned objects are both instances of this class and

Is an object class instance.

Code example:

Function person (name)

{

This. Name = Name;

}

VaR P = new person ('zhang san ');

If (P instanceof Person)

{

Alert ("P is an instance of person ");

}

If (P instanceof object)

{

Alert ("P is an object instance ");

}

The output result is: "P is an instance of person", and "P is an instance of object ".

 

13.10.2 objects and associated Arrays

Objects in JavaScript are essentially an associated array, or more similar to the data structure of Java redmap.

Key-value pairs. Therefore, when you need to access the attributes of a JavaScript Object, you can not only use obj. propname

You can also use OBJ [propname.

Code example:

Function person (name)

{

This. Name = Name;

}

VaR P = new person ("James ");

Alert (P. Name );

Output result: Zhang San.

 

13.10.3 inheritance and prototype

In an object-oriented programming language, classes and classes have explicit inheritance relationships. A class can be explicitly inherited.

From which class, the subclass will have all attributes and methods of the parent class. Although JavaScript supports the concepts of classes and objects

There is no concept of inheritance, and only one special method can be used to expand the original JavaScript class.

This special method is a function. All classes in Javascript have a prototype attribute.

When adding functions and attributes to the prototype attribute of, it can be considered as an extension of the original class. We can understand that prototype is added.

The property class inherits the original class-this is the pseudo-Inheritance Mechanism provided by JavaScript.

Code example:

// Define a person function and the person class

Function person (name, age)

{

This. Name = Name;

This. Age = age;

This.info = function ()

{

Document. writeln ("name:" + this. Name );

Document. writeln ("Age:" + this. Age );

}

}

Create person instance p1

VaR p1 = new person ("Zhang San", 27 );

// The walk method cannot be called here. The variable P1 does not have a walk method.

// Add the Walk Method to the prototype attribute of person

Person. Prototype. Walk = function ()

{

Document. writeln (this. Name + "walking slowly ");

}

// Here P1 inherits the new method walk

// Call the info and walk Methods

P1.info ();

P1.walk ();

 

Output result: Name: Zhang San age: 27 Zhang San is walking slowly.

 

    

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.