This article mainly introduces javascript object-oriented code examples, including classes, static members, objects, and overloading. For more information, see
I. Use of basic classes
Method 1:
The 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:
The Code is as follows:
Function something ()
{
This. a =;
This. output = function ()
{
Document. write (this. );
}
}
Var s = new something (2 );
S. output (); // output 2
Ii. Inheritance
Method 1:
The 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:
The 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 ,"
");
}
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.
The 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 ,"
");
}
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 ,"
");
}
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
The Code is as follows:
Function something ()
{
This. a =;
}
Something. fun = function (s)
{
Document. write (s. );
}
Var s = new something (2 );
Something. fun (s); // output 2
4. Release objects
The 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
The 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
The 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
The 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"
The Code is as follows:
Function fun ()
{
Var v = 1;
Function fun2 ()
{
++ V;
Document. write (v );
Document. write ("
");
Return v;
}
Return fun2;
}
Var func = fun ();
Func (); // output 2
Func (); // output 3
Func (); // output 4