Home  >  Article  >  Web Front-end  >  JS implements resend_javascript technique of self

JS implements resend_javascript technique of self

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

ECMA V5 defines a long-awaited method: Object.getPrototypeOf, which can get the prototype of an object ([[prototype]]) regardless of type information. Based on this, we can construct a resend: (Please use Chrome 5, IE9 preview third version test)

Copy code The code is as follows:

obj.resend = function () {
var pof = Object.getPrototypeOf;
var has = function() {......} // hasOwnProperty encapsulation
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;
}
}()
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn