javascript面向?qū)ο筇匦源a實(shí)例
一、基本的類的使用
方法一:
function sth(a) // 構(gòu)造函數(shù)
{
this.a = a;
this.fun = output; // 成員函數(shù)
}
function output(a, b, c)
{
document.write(this.a);
}
//調(diào)用
var s = new sth(250);
s.fun(1, 2, 3);
ouput(1, 2, 3); //如果output在sth之前就是錯的
方法二:
{
this.a = a;
this.output = function()
{
document.write(this.a);
}
}
var s = new sth(2);
s.output(); // 輸出2
二、繼承
方法一:
{
this.x = x;
}
function B(x, y)
{
// 方法1
/*
this.construct = A;
this.construct(x);
delete this.construct;
*/
// 方法2
//A.call(this, x);
// 方法3
A.apply(this, new Array(x)); // 亦可A.apply(this, arguments), 不過arguments參數(shù)順序一定要對
this.y = y;
this.print = function()
{
document.write("x = ", x,
", y = ", y);
}
}
var b = new B(1, 2);
b.print();
alert(B instanceof A); // 輸出false
優(yōu)點(diǎn):可以實(shí)現(xiàn)多繼承(多調(diào)用call就好)
缺點(diǎn):
· 必須以構(gòu)造函數(shù)方式使用
· 使用instanceof運(yùn)算符運(yùn)算此類繼承結(jié)果為false
方法二:
{
}
A.prototype.x = 1;
function B()
{
}
B.prototype = new A(); // 不能帶參數(shù)!
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); // 輸出true
缺點(diǎn):
· 不能實(shí)現(xiàn)多繼承
· 構(gòu)造函數(shù)不帶參數(shù)
Tips
通常使用混合模式,兩者一起用
function A(x)
{
this.x = x;
}
A.prototype.printx = function() // 寫到A類里面this.printx = function....也是可以的,下同
{
document.write(this.x, "<br>");
}
function B(x, y)
{
A.call(this, x);
this.y = y;
}
B.prototype = new A(); // 不能帶參數(shù)!
B.prototype.printxy = function()
{
document.write(this.x, ", ", this.y, "<br>");
}
var b = new B(1, 2);
b.printx(); // 輸出1
b.printxy(); // 輸出1, 2
document.write(b instanceof A); // 輸出true
三、類似靜態(tài)成員函數(shù)的使用
{
this.a = a;
}
sth.fun = function(s)
{
document.write(s.a);
}
var s = new sth(2);
sth.fun(s); // 輸出2
四、對象的釋放
obj = null; // 取消引用,會自動進(jìn)行垃圾回收;如果需要根本釋放此對象,要將它的所有引用都賦值為null
五、函數(shù)對象
v(1, 2); // 將會輸出3
六、回調(diào)函數(shù)
{
func(arg);
}
function fun(arg)
{
document.write(arg);
}
//callback(func, "sb"); // 這種做法不行
var func = new Function("arg", "fun(arg);");
// 當(dāng)然也可以把func(arg)換成具體的執(zhí)行代碼,
// 但是函數(shù)代碼龐大了就最好這樣做了
callback(func, "sb");
七、函數(shù)的重載
{
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);
八、利用函數(shù)閉包實(shí)現(xiàn)有“靜態(tài)變量”的函數(shù)
{
var v = 1;
function fun2()
{
++v;
document.write(v);
document.write("<br>");
return v;
}
return fun2;
}
var func = fun();
func(); // 輸出2
func(); // 輸出3
func(); // 輸出4
相關(guān)文章
eval(function(p,a,c,k,e,d)系列解密javascript程序
在網(wǎng)上下載源代碼時,很可能發(fā)現(xiàn)代碼里的JS腳本看不懂,這是由于JS加密造成的。如果你發(fā)現(xiàn)JS是以eval(function(p,a,c,k,e,r){e=function(c)開頭的,看到這個頁面你就可以解決他2007-04-04
mpvue實(shí)現(xiàn)小程序簽到金幣掉落動畫(api實(shí)現(xiàn))
這篇文章主要介紹了mpvue實(shí)現(xiàn)小程序簽到金幣掉落動畫,這里使用小程序自帶的api來實(shí)現(xiàn),文中通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-10-10
JS中檢測數(shù)據(jù)類型的幾種方式及優(yōu)缺點(diǎn)小結(jié)
這篇文章主要介紹了JS中檢測數(shù)據(jù)類型的幾種方式及優(yōu)缺點(diǎn)小結(jié),非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
微信小程序搜索框樣式并實(shí)現(xiàn)跳轉(zhuǎn)到搜索頁面(小程序搜索功能)
這篇文章主要介紹了微信小程序搜索框樣式并實(shí)現(xiàn)跳轉(zhuǎn)到搜索頁面(小程序搜索功能),需要的朋友可以參考下2020-03-03
javascript創(chuàng)建數(shù)組的最簡代碼
2008-02-02

