The use of basic classes
Method A
function Sth (a)//constructor
{
THIS.A = A;
this.fun = output; 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 is wrong before the STH
Method Two
function Sth (a)
{
THIS.A = A;
this.output = function ()
{
document.write (THIS.A);
}
}
var s = new Sth (2);
S.output (); Output 2
Inheritance
Method A
function A (x)
{
this.x = x;
}
function B (x, y)
{
//Method 1
/*
this.construct = A;
this.construct (x);
Delete this.construct;
*/
//Method 2
//a.call (this, x);
//Method 3
a.apply (This, new Array (x)); may also a.apply (this, arguments), but the arguments parameter order must be to
this.y = y;
this.print = function ()
{
document.write ("x =", X,
", y =", y);
}
}
var b = new B (1, 2);
B.print ();
alert (B instanceof A); Output False
Benefits: Multiple inheritance can be implemented (call is good for multiple calls)
Disadvantage:
·
must be used as a constructor
· Using the instanceof operator, this class inherits the result to False
Method Two
function A ()
{
}
a.prototype.x = 1;
function B ()
{
}
B.prototype = new A (); Can't take 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); Output true
Disadvantage:
· Cannot implement multiple inheritance
· constructor with no parameters
Tips
typically use a blending mode, with both
function A (x)
{
this.x = x;
}
a.prototype.printx = function ()//write to class A This.printx = function .... is also possible, the same as
{
document.write (this.x, "<br>");
}
function B (x, y)
{
A.call (this, x);
this.y = y;
}
B.prototype = new A (); Can't take 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); Output true
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); Output 2
The release of the
object
var obj = new Object; obj is a reference
obj = null; A dereference is automatically garbage collected, and if you need to free this object at all, assign all its references to null
Function Object
var v = new Function ("Arg1", "arg2", "document.write (Arg1 + arg2);"); Defines a function object, which is Arg1,arg2
V (1, 2); 3
will be output
callback function
function Callback (func, Arg)
{
func (ARG);
}
function Fun (ARG)
{
document.write (ARG);
}
//callback (func, "SB"); This can't be done
var func = new Function ("Arg", "Fun (ARG);");
///Of course, you can also convert func (ARG) into specific execution code,
But the function code is huge, it's best to do it
Callback (func, "SB");
Overloaded
for
functions
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);
uses function closure 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 (); Output 2
func (); Output 3
func (); Output 4