Basic object-oriented Javascript

Source: Internet
Author: User

Reference: http://bbs.51js.com/thread-74195-1-1.html

Constructor

ConstructorCodeIt must be placed at the end of the class definition to ensure that all methods called in the class have been defined.

 

Private instance Member [variables and methods]

Private instance members can be implemented using local variables in the function in Javascript, which is equivalent to private instance members of the class.

They can only be used inside the class object and cannot be used outside the class object.

There are two ways to create a private method. One is to define a method directly in the class, and the other is to define a local variable (Private instance field) first ), then assign an anonymous method to it.

 

Public instance members: the prototype and this methods are defined.

1. The prototype method should only be defined outside the class. This method can only be defined in the class.

2. If prototype is defined in a class, when a private instance Member is accessed, the private instance Member in the last object instance is always accessed.

3. A public instance Member defined in prototype mode is a member created on the prototype of the class. A public instance Member defined in this mode is a member directly created on the Instance Object of the class.

Based on the two differences, we can conclude that to access private instance members in the public instance method, this must be defined.

For the third difference, we will further analyze it when discussing inheritance. You only need to know the difference here.

Do not define the public instance method created using prototype in the class!

 

Public static member

You can assign a value to classname. membername.

Do not define public static members inside the class where they are located

 

Static class

Myclass = new function (){...}

 

Inheritance: one is the prototype inheritance method, and the other is the inheritance method.

[Prototype inheritance law]

In the public instance method inherited by subclass, if the private instance field or private instance method is called, The called Private instance members belong to the parent class.

If the private instance field or private instance method is called, The called Private instance members belong to the subclass.

The method defined on the parent class prototype will inherit the quilt class.

The instance method defined in the subclass cannot access the private instance members defined in the parent class.

Static members are not inherited.

 

[Call the inheritance law]

The method defined on the parent class prototype does not inherit from the quilt class.

The instance method defined in the subclass cannot access the private instance members defined in the parent class.

Static members will not be inherited.

Multiple inheritance can be implemented by calling the inheritance method. That is to say, a subclass can inherit from multiple parent classes all the public instance members defined in the parent class by using this method.

Function subclass (){

// Inherit

Parentclass. Call (this );

 

Method overload example

Function parentclass (){

This. method = function (){

Alert ("parentclass method ");

}

}

Function subclass (){

VaR method = This. method;

This. method = function (){

Method. Call (this );

Alert ("subclass method ");

}

}

Subclass. Prototype = new parentclass ();

Subclass. Prototype. constructor = subclass;

 

VaR o = new subclass ();

O. Method ();

 

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.