有人问我一个有趣的问题,我不知道该怎么做。我会感谢任何帮助。覆盖的Javascript本地方法,并调用超
问:当你打电话Array.push()
,应该推动正常,也应该对自定义函数的调用。
这里是我的尝试:
Array.prototype.push = function() {
Array.prototype.push.call(this, arguments);
if(typeof customMethod === 'function') {
customMethod();
}
}
function customMethod() {
console.log('customMethod called');
}
但是,这是行不通的。
凡/如何'customMethod'定义?当你的代码现在出现时,它将是未定义的。 –
你重写'push'而不保存原来的一个... – Hacketo