JS實現(xiàn)self的resend
更新時間:2010年07月22日 01:00:42 作者:
self中resend是調(diào)用“基類方法”的原語,它會把當(dāng)前接收到的消息原樣發(fā)送給其原型(parent*)。在ECMA-v5時代,我們終于可以做出這個偉大的東西了。
ECMA V5定義了一個期待已久的方法:Object.getPrototypeOf,它可以無視型別信息得到某對象的原型([[prototype]]),基于此,我們可以構(gòu)造出一個resend:(請用Chrome 5、IE9預(yù)覽第三版測試)
obj.resend = function() {
var pof = Object.getPrototypeOf;
var has = function() {......} // hasOwnProperty的封裝
var make = function(obj, old) {
return function(name, args) {
var step = pof(obj),
r;
while (step && !has(step, name)) step = pof(step);
if (!step) throw new Error('Unable to resend: method missing');
var foundMethod = step[name];
var backup = arguments.callee;
this.resend = make(this, backup);
r = foundMethod.apply(this, Array.prototype.slice.call(arguments, 1));
this.resend = old;
return r
}
};
return function(name, args__) {
var rv;
var old = this.resend;
this.resend = make(this, old);
rv = this.resend.apply(this, arguments);
this.resend = original;
return rv;
}
}()
復(fù)制代碼 代碼如下:
obj.resend = function() {
var pof = Object.getPrototypeOf;
var has = function() {......} // hasOwnProperty的封裝
var make = function(obj, old) {
return function(name, args) {
var step = pof(obj),
r;
while (step && !has(step, name)) step = pof(step);
if (!step) throw new Error('Unable to resend: method missing');
var foundMethod = step[name];
var backup = arguments.callee;
this.resend = make(this, backup);
r = foundMethod.apply(this, Array.prototype.slice.call(arguments, 1));
this.resend = old;
return r
}
};
return function(name, args__) {
var rv;
var old = this.resend;
this.resend = make(this, old);
rv = this.resend.apply(this, arguments);
this.resend = original;
return rv;
}
}()
相關(guān)文章
詳談js中標(biāo)準(zhǔn)for循環(huán)與foreach(for in)的區(qū)別
下面小編就為大家?guī)硪黄斦刯s中標(biāo)準(zhǔn)for循環(huán)與foreach(for in)的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11js實現(xiàn)網(wǎng)頁倒計時、網(wǎng)站已運行時間功能的代碼3例
這篇文章主要介紹了js實現(xiàn)網(wǎng)頁倒計時、網(wǎng)站已運行時間功能的代碼3例,需要的朋友可以參考下2014-04-04JS數(shù)組操作大全對象數(shù)組根據(jù)某個相同的字段分組
這篇文章主要介紹了JS數(shù)組操作大全對象數(shù)組根據(jù)某個相同的字段分組,需要注意的是,在開發(fā)過程這種數(shù)組的處理函數(shù),應(yīng)當(dāng)被編寫到項目的公共工具函數(shù)庫中全局調(diào)用,本文結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-11-11JSON創(chuàng)建鍵值對(key是中文或者數(shù)字)方式詳解
這篇文章主要介紹了JSON創(chuàng)建鍵值對(key是中文或者數(shù)字)方式詳解,需要的朋友可以參考下2017-08-08JS使用window.requestAnimationFrame()對列表切片進行渲染
這篇文章主要為大家介紹了JS使用requestAnimationFrame對列表切片進行渲染,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05

javascript簡單拖拽實現(xiàn)代碼(鼠標(biāo)事件 mousedown mousemove mouseup)
javascript簡單拖拽,簡單拖拽實現(xiàn)
2012-05-05