Those who have learned java, c#, and vb know the concept of classes, and classes have functions such as inheritance, encapsulation, and polymorphism. And javascript is not an object-oriented language, it is an interpretive language.
But we can also use javascript to achieve inheritance and polymorphism.
There are many ways to implement javascript classes.
Method 1: Constructing method.
Code
function coder(){
= 'Xiao Wang';
= 'Programmer';
= function ()
{
alert('I'm writing code');
}
}
var coder=new coder();
alert();
();
Method 2: Factory method.
Code
function createCoderFactory(){
var obj=new Object();
= 'Xiao Wang';
= 'Programmer';
= function (){
alert('I'm writing code');
};
return obj;
}
var coder = createCoderFactory();
alert();
();
However, the factory method and the construction method have the same disadvantage, that is, every time an instance is created, each function of the class will be instantiated.
Method 3: Prototype chain.
Code
function coder(){}
= 'Xiao Wang';
= 'Programmer';
= function(){
alert('I'm writing code');
};
var coder = new coder();
alert();
();
Note: The book says: A disadvantage of the prototype chain is that all its attributes are shared, and as long as one instance changes, others will change. The tests are as follows:
var coder1 = new coder();
var coder2 = new coder();
alert(); /*Show "Xiao Wang"*/
= 'Lao Wang';
alert(); /*This displays "Xiao Wang" If the book says it should display "Lao Wang"*/
alert(); /*This displays "Lao Wang"*/
alert();If the book says "Lao Wang", but the book shows "Xiao Wang", so there is an error.
Method 4: Mixed method.
The above three have their own shortcomings, so we need to improve them.
function coder(){
= 'Xiao Wang';
= 'Programmer';
}
= function(){
alert('I'm writing code');
};
Method 5: Dynamic original chain.
There is another way to solve the shortcomings of the first three.
Code
function coder(){
= 'Xiao Wang';
= 'Programmer';
if (typeof(coder._init) == 'undefined'){
= function ()
{
alert('I'm writing code');
};
this._init = true;
}
}
As for this method, when used for the first time, since _init is not initialized, the following code will be executed to instantiate the coding function. It will not be executed again in the future, so that the function will be instantiated only once.
But we can also use javascript to achieve inheritance and polymorphism.
There are many ways to implement javascript classes.
Method 1: Constructing method.
Code
Copy the codeThe code is as follows:
function coder(){
= 'Xiao Wang';
= 'Programmer';
= function ()
{
alert('I'm writing code');
}
}
var coder=new coder();
alert();
();
Method 2: Factory method.
Code
Copy the codeThe code is as follows:
function createCoderFactory(){
var obj=new Object();
= 'Xiao Wang';
= 'Programmer';
= function (){
alert('I'm writing code');
};
return obj;
}
var coder = createCoderFactory();
alert();
();
However, the factory method and the construction method have the same disadvantage, that is, every time an instance is created, each function of the class will be instantiated.
Method 3: Prototype chain.
Code
Copy the codeThe code is as follows:
function coder(){}
= 'Xiao Wang';
= 'Programmer';
= function(){
alert('I'm writing code');
};
var coder = new coder();
alert();
();
Note: The book says: A disadvantage of the prototype chain is that all its attributes are shared, and as long as one instance changes, others will change. The tests are as follows:
Copy the codeThe code is as follows:
var coder1 = new coder();
var coder2 = new coder();
alert(); /*Show "Xiao Wang"*/
= 'Lao Wang';
alert(); /*This displays "Xiao Wang" If the book says it should display "Lao Wang"*/
alert(); /*This displays "Lao Wang"*/
alert();If the book says "Lao Wang", but the book shows "Xiao Wang", so there is an error.
Method 4: Mixed method.
The above three have their own shortcomings, so we need to improve them.
Copy the codeThe code is as follows:
function coder(){
= 'Xiao Wang';
= 'Programmer';
}
= function(){
alert('I'm writing code');
};
Method 5: Dynamic original chain.
There is another way to solve the shortcomings of the first three.
Code
Copy the codeThe code is as follows:
function coder(){
= 'Xiao Wang';
= 'Programmer';
if (typeof(coder._init) == 'undefined'){
= function ()
{
alert('I'm writing code');
};
this._init = true;
}
}
As for this method, when used for the first time, since _init is not initialized, the following code will be executed to instantiate the coding function. It will not be executed again in the future, so that the function will be instantiated only once.