Heim  >  Artikel  >  Web-Frontend  >  JS实现self的resend_javascript技巧

JS实现self的resend_javascript技巧

WBOY
WBOYOriginal
2016-05-16 18:22:371151Durchsuche

ECMA V5定义了一个期待已久的方法:Object.getPrototypeOf,它可以无视型别信息得到某对象的原型([[prototype]]),基于此,我们可以构造出一个resend:(请用Chrome 5、IE9预览第三版测试)

复制代码 代码如下:

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;
}
}()
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn