Share JavaScript Object-oriented Inheritance

Source: Internet
Author: User

1. Large text block declaration in Javascript

1 $ (Document). Ready (
2 Function ()
3 {
4 VaR Str =   ' \
5 Helo me fdsaf \
6 Me \
7 \
8 Test \
9 ' ;
10 Alert (STR );
11 }
12 );
13

 

This and @ Willow once encountered this problem.

2. Douglas crockford implements an ingenious inheritance of class patterns. Added some annotations for easy understanding.

Understanding the basic knowledge required for this function

(1) Add a public method to function. prototype. All functions extended by classes can use it. It has a name and a function, and adds them to the function'sPrototype.

(2) If, while, the statements that need to judge the function, the number 0 as the condition to convert to false,> 0 to true.

 

Implement inherited functions

// Auxiliary Methods
Function. Prototype. Method =   Function (Name, func ){
This . Prototype [name] = Func;
Return   This ;
};
// Complex Functions =
// You can implement quick inheritance. You can also selectively call the function of the parent object.
Function. Method ( ' Inherits ' , Function (Parent ){
VaR D = {};
This . Prototype =   New Parent ();
VaR P =   This . Prototype;
// A privileged method can call the method of the parent class.
This . Method ( ' Uber ' , Function Uber (name ){
// Determine whether the method exists
If ( ! (Name In D ))
{
D [name] =   0 ;
}
VaR F, R, T = D [name], V = Parent. Prototype;
// Executed?
If (T)
{
// If you have performed
While (T)
{
V = V. constructor. Prototype;
T -=   1 ;
}
F = V [name];
}
// First execution
Else
{
F = P [name];
// If you point to the method of the current prototype, call the method of the parent object
If (F =   This [Name])
{
F = V [name];
}
}
// Clever and disgusting call stack records
D [name] + =   1 ; // This function is also clever. arguments is not a real array, so it must be used againApplyTo call the ArraySlice. R = F. Apply ( This , Array. Prototype. Slice. Apply (arguments ,[ 1 ]);
Alert (d [name]);
Return R;
});
Return   This ;
});
// Only the auxiliary functions that inherit the specific functions of the parent class
Function. Method ( ' Swiss ' , Function (Parent ){
For ( VaR I =   1 ; I < Arguments. length; I + =   1 )
{
VaR Name = Arguments;
This . Prototype [name] = Parent. Prototype [name];
}
Return   This ;
}

 

 

 

 

Call Code

Function User (){
This . Name =   " User " ;
}
User. Method ( " Sayyouname " , Function () {Alert ( " From: "   +   This . Name );});
Function MAN (){
This . Name =   " Man " ;
}
Man. inherits (User );
Man. Method ( " Sayyouname " ,
Function (){
This . Uber ( " Sayyouname " );
Alert ( " And I am man " );
});

3. class inheritance of Dean Edwards base. js

The class inheritance method is more elegant. If the method is overwritten, you only need to call this. base. Do not show the write method name

 

Code

// Base. js begin
VaR star = Base. Extend ({
Constructor: function (name ){
This . Name = Name;
},
Name: "" ,
Say: function (Message ){
Alert ( This . Name +   " : "   + Message );
}
});

VaR sun=Star. Extend ({
Say: function (Message ){
This.Base(Message );
Alert ("Base over");
}
});

 

 

CallCode

 

( New Sun ( " Tom " ). Say ( " Hello world! " );

 

 

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.