I. Use of basic classes
Method 1:
Copy codeThe Code is as follows:
Function something (a) // Constructor
{
This. a =;
This. fun = output; // member function
}
Function output (a, B, c)
{
Document. write (this. );
}
// Call
Var s = new something (250 );
S. fun (1, 2, 3 );
Ouput (1, 2, 3); // If the output is wrong before something
Method 2:
Copy codeThe Code is as follows: function… ()
{
This. a =;
This. output = function ()
{
Document. write (this. );
}
}
Var s = new something (2 );
S. output (); // output 2
Ii. Inheritance
Method 1:
Copy codeThe Code is as follows: function A (x)
{
This. x = x;
}
Function B (x, y)
{
// Method 1
/*
This. construct =;
This. construct (x );
Delete this. construct;
*/
// Method 2
// A. call (this, x );
// Method 3
A. apply (this, new Array (x); // it can also be A. apply (this, arguments), but the arguments Parameter order must be
This. y = y;
This. print = function ()
{
Document. write ("x =", x,
", Y =", y );
}
}
Var B = new B (1, 2 );
B. print ();
Alert (B instanceof A); // outputs false
Advantage: Multi-inheritance can be implemented (it is good to call multiple)
Disadvantages:
· Must be used as a constructor
· The result of using the instanceof operator to calculate such inheritance is false.
Method 2:
Copy codeThe Code is as follows: function ()
{
}
A. prototype. x = 1;
Function B ()
{
}
B. prototype = new A (); // parameters are not allowed!
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); // Output true
Disadvantages:
· Multi-inheritance is not allowed
· Constructors without Parameters
Tips
Generally, the hybrid mode is used.
Copy codeThe Code is as follows:
Function A (x)
{
This. x = x;
}
A. prototype. printx = function () // write it to class A. this. printx = function... is also acceptable, the same below
{
Document. write (this. x, "<br> ");
}
Function B (x, y)
{
A. call (this, x );
This. y = y;
}
B. prototype = new A (); // parameters are not allowed!
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); // Output true
Iii. Use of static member functions
Copy codeThe Code is as follows: function… ()
{
This. a =;
}
Something. fun = function (s)
{
Document. write (s. );
}
Var s = new something (2 );
Something. fun (s); // output 2
4. Release objects
Copy codeThe Code is as follows: var obj = new Object; // obj is a reference
Obj = null; // if the object is not referenced, garbage collection is automatically performed. If you want to release the object, assign null values to all its references.
V. Function objects
Copy codeThe Code is as follows: var v = new Function ("arg1", "arg2", "document. write (arg1 + arg2); "); // defines a function object. The parameters are arg1 and arg2.
V (1, 2); // output 3
Vi. Callback Functions
Copy codeThe Code is as follows: function callback (func, arg)
{
Func (arg );
}
Function fun (arg)
{
Document. write (arg );
}
// Callback (func, "sb"); // This is not the case
Var func = new Function ("arg", "fun (arg );");
// Of course, you can also change func (arg) to a specific Execution Code,
// But it is best to do this if the function code is huge.
Callback (func, "sb ");
VII. Function Overloading
Copy codeThe Code is as follows: 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 );
8. Use Function closures to implement functions with "static variables"
Copy codeThe Code is as follows: function fun ()
{
Var v = 1;
Function fun2 ()
{
++ V;
Document. write (v );
Document. write ("<br> ");
Return v;
}
Return fun2;
}
Var func = fun ();
Func (); // output 2
Func (); // output 3
Func (); // output 4