javascript call和apply方法
更新時間:2008年11月24日 13:48:29 作者:
用于改變方法的當前對象
例子如下:
<script>
/**
*動物
*/
function Animal(){
this.name='Amimal';
this.showName=function(){
alert(this.name);
};
}
/*
*貓
*/
function Cat(){
this.name='cat';
}
var animal=new Animal;//創(chuàng)建動物對象
var cat=new Cat;//創(chuàng)建貓對象
animal.showName.call(cat,'','');//輸出cat,說明showName函數(shù)的當前this已經(jīng)變?yōu)閏at了
animal.showName.apply(cat,[]);//輸出cat
//call函數(shù)和apply函數(shù)的區(qū)別是call 的語法是function.call(obj,param1,param2……);applay的語法是function.call(obj,[]/*params[]參數(shù)數(shù)組*/);
</script>
復制代碼 代碼如下:
<script>
/**
*動物
*/
function Animal(){
this.name='Amimal';
this.showName=function(){
alert(this.name);
};
}
/*
*貓
*/
function Cat(){
this.name='cat';
}
var animal=new Animal;//創(chuàng)建動物對象
var cat=new Cat;//創(chuàng)建貓對象
animal.showName.call(cat,'','');//輸出cat,說明showName函數(shù)的當前this已經(jīng)變?yōu)閏at了
animal.showName.apply(cat,[]);//輸出cat
//call函數(shù)和apply函數(shù)的區(qū)別是call 的語法是function.call(obj,param1,param2……);applay的語法是function.call(obj,[]/*params[]參數(shù)數(shù)組*/);
</script>
相關(guān)文章
驗證控件與Button的OnClientClick事件詳細解析
以下就是被我已知忽略的問題和解決方案,當我發(fā)覺這個問題的時候,冒出了一身冷汗,幸虧做了嚴格的服務器端驗證,不然可就慘了2013-12-12
JavaScript創(chuàng)建類/對象的幾種方式概述及實例
JS中的對象強調(diào)的是一種復合類型,JS中創(chuàng)建對象及對對象的訪問是極其靈活的,下面與大家分享下創(chuàng)建類/對象的幾種方式,感興趣的朋友可以了解下哈2013-05-05

