Javascript object oriented feature code instance

  • 2020-03-30 03:18:19
  • OfStack

One, the use of basic classes
Method one:


function sth(a) //The constructor
{ 
 this.a = a; 
 this.fun = output; //A member function
} 
function output(a, b, c) 
{ 
 document.write(this.a); 
} 
// call 
var s = new sth(250); 
s.fun(1, 2, 3); 
ouput(1, 2, 3); // if output in sth It was wrong before 

Method 2:

function sth(a) 
{   
 this.a = a; 
 this.output = function() 
 { 
  document.write(this.a); 
 } 
} 

var s = new sth(2); 
s.output(); //  The output 2 

Second, inheritance,
Method one:
function A(x) 
{ 
 this.x = x; 
} 
function B(x, y) 
{ 
 //Method 1
   

 //Method 2
 //A.call(this, x); 

 //Methods 3
 A.apply(this, new Array(x)); //A. ply(this, arguments) is also acceptable, but the arguments must be in the correct order

 this.y = y; 
 this.print = function() 
 { 
  document.write("x = ", x, 
         ", y = ", y); 
 } 
} 
var b = new B(1, 2); 
b.print();
alert(B instanceof A); //  The output false

Advantages: multiple inheritance is possible (multiple calls are good)

Disadvantages:
, must be used as a constructor
, use the instanceof operator to calculate the result of this inheritance to be false

Method 2:

function A() 
{ 
} 
A.prototype.x = 1; 

function B() 
{ 
} 
B.prototype = new A(); //No parameters!
B.prototype.y = 2;  
B.prototype.print = function() 
{ 
 document.write(this.x, ", ", this.y, "<br>"); 
} 

var b = new B(); 
b.print(); 
document.write(b instanceof A); //The output of true

Disadvantages:
, multiple inheritance cannot be implemented
, constructors take no arguments

Tips

Usually a mixed mode is used and both are used together


function A(x) 
{ 
 this.x = x; 
} 
A.prototype.printx = function()  //In class A, this.printx = function... That's fine. Same thing
{ 
 document.write(this.x, "<br>"); 
} 

function B(x, y) 
{ 
 A.call(this, x); 
 this.y = y; 
} 
B.prototype = new A(); //No parameters!  
B.prototype.printxy = function() 
{ 
 document.write(this.x, ", ", this.y, "<br>"); 
} 

var b = new B(1, 2); 
b.printx();  //Output 1
b.printxy(); //Output 1, 2
document.write(b instanceof A); //  The output true

Third, similar to the use of static member functions

function sth(a) 
{   
 this.a = a; 
} 

sth.fun = function(s) 
{ 
 document.write(s.a); 
} 

var s = new sth(2); 
sth.fun(s); //  The output 2


Release of objects

var obj = new Object; //Obj is quoted
obj = null; //  Dereference is automatically garbage collected; If you need to release the object altogether, assign all its references to null

Five, function object

var v = new Function("arg1", "arg2", "document.write(arg1 + arg2);"); //Define a function object with arguments arg1 and arg2
v(1, 2); //  Will be output 3

The callback function

function callback(func, arg) 
{ 
 func(arg); 
} 

function fun(arg) 
{ 
 document.write(arg); 
}

//callback(func, "sb"); //  That's not gonna work  

var func = new Function("arg", "fun(arg);"); 
//Func (arg) can also be replaced by a specific execution code, & NBSP;    
//But this is best done when the function code is large
callback(func, "sb");

Seven, function overload

function fun() 
{ 
 switch (arguments.length) 
 { 
 case 1: 
  document.write(arguments[0]); 
  break; 
 case 2: 
  document.write(arguments[0] + arguments[1]); 
  break; 
 default: 
  document.write("ERROR!"); 
  break; 
 } 
} 

fun(1); 
fun(1, 2);

Using function closures to implement functions with "static variables"

function fun() 
{ 
 var v = 1; 
 function fun2() 
 { 
  ++v; 
  document.write(v); 
  document.write("<br>"); 
  return v; 
 } 

 return fun2; 
} 

var func = fun(); 
func(); //The output of 2
func(); //The output of 3
func(); //The output of 4


Related articles: