Home > Web Front-end > JS Tutorial > body text

JS implements resend_javascript technique of self

WBOY
Release: 2016-05-16 18:22:37
Original
1166 people have browsed it

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;
}
}()
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!