JavaScript Object-oriented attribute code instance _javascript tips

Source: Internet
Author: User
Tags garbage collection

First, the use of basic classes
Method One:

Copy Code code as follows:

function Sth (a)//constructor
{
THIS.A = A;
This.fun = output; member functions
}

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 sth,

Method Two:

Copy Code code as follows:
function Sth (a)
{
THIS.A = A;
This.output = function ()
{
document.write (THIS.A);
}
}

var s = new Sth (2);
S.output (); Output 2

Second, the succession
Method One:
Copy Code code as follows:
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 calls are good)

Disadvantages:
· Must be used as a constructor
· Using the instanceof operator, this class inherits the result to False

Method Two:

Copy Code code as follows:
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


Disadvantages:
· Cannot implement multiple inheritance
· constructor with no parameters

Tips

Blending mode is usually used, both

Copy Code code as follows:

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

Third, the use of similar static member functions

Copy Code code as follows:
function Sth (a)
{
THIS.A = A;
}

Sth.fun = function (s)
{
document.write (S.A);
}

var s = new Sth (2);
Sth.fun (s); Output 2


Iv. release of objects

Copy Code code as follows:
var obj = new Object; obj is a reference
obj = null; Dereference, garbage collection is done automatically, and if you need to dispose of this object at all, assign all its references to null

Five, Function object

Copy Code code as follows:
var v = new Function ("Arg1", "arg2", "document.write (Arg1 + arg2);"); Defines a function object, which is ARG1,ARG2
V (1, 2); Will output 3

Six, callback function

Copy Code code as follows:
function callback (func, Arg)
{
Func (ARG);
}

function Fun (ARG)
{
document.write (ARG);
}

Callback (func, "SB"); That's not going to work.

var func = new Function ("Arg", "Fun (ARG);");
Of course, you can also change func (ARG) into specific execution code,
But the function code is huge, and that's the best thing to do.
Callback (func, "SB");

Overloading of functions

Copy Code code 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);

Implementation of functions with "static variables" using function closures

Copy Code code 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.