JS實(shí)現(xiàn)self的resend
更新時(shí)間:2010年07月22日 01:00:42 作者:
self中resend是調(diào)用“基類方法”的原語,它會(huì)把當(dāng)前接收到的消息原樣發(fā)送給其原型(parent*)。在ECMA-v5時(shí)代,我們終于可以做出這個(gè)偉大的東西了。
ECMA V5定義了一個(gè)期待已久的方法:Object.getPrototypeOf,它可以無視型別信息得到某對(duì)象的原型([[prototype]]),基于此,我們可以構(gòu)造出一個(gè)resend:(請(qǐng)用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)文章
如何使用electron-builder及electron-updater給項(xiàng)目配置自動(dòng)更新
這篇文章主要介紹了如何使用electron-builder及electron-updater給項(xiàng)目配置自動(dòng)更新,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
JS獲取地址欄參數(shù)的兩種方法(簡單實(shí)用)
這篇文章主要介紹了JS獲取地址欄參數(shù)的兩種方法(簡單實(shí)用),小編給大家推薦使用第一種采用正則表達(dá)式獲取地址欄參數(shù)的方法,此方法簡單實(shí)用,對(duì)js獲取地址欄參數(shù)相關(guān)知識(shí)感興趣的朋友,一起學(xué)習(xí)吧2016-06-06
JavaScript獲取URL參數(shù)的四種方法總結(jié)
在前端開發(fā)過程中難免會(huì)遇到處理url參數(shù)的問題,這篇文章主要給大家總結(jié)介紹了關(guān)于JavaScript獲取URL參數(shù)的四種方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01

