var shell = function (method) {
window[method].apply(null, Array.prototype.slice.call(arguments, 1));
};
shell('alert', 'monkey!');
var shell = function (method) {
window[method].apply(null, Array.prototype.slice.call(arguments, 1));
};
shell('alert', 'monkey!');
alert.apply是问题所在。alert不是语言的一部分,因此它可能甚至不是一个函数。它是实现相关的。
window
е’ҢDOMе…ғзҙ дёҠеҗ‘JavascriptејҖж”ҫAPIпјҢдҪҶжҳҜиҝҷз§ҚжҡҙйңІжҳҜжңүйҷҗеҲ¶зҡ„пјҢйҖҡеёёдёҚиғҪеғҸеӨ„зҗҶвҖңзңҹжӯЈвҖқзҡ„Javascript组件дёҖж ·еӨ„зҗҶе®ғ们гҖӮ - Pointyvar shell = function (method) {
var fn = window[method];
fn.apply = Function.prototype.apply;
fn.apply(null, Array.prototype.slice.call(arguments, 1));
};
shell('alert', 'monkey!');
- shawndumaswindow
是一个宿主对象,而alert
是宿主对象的属性。宿主对象不一定要像原生对象那样表现。以下问题与您的类似:http://stackoverflow.com/questions/3060318/js-proxy-pattern-problem - Tim Down
Array.prototype.slice
。 - Pointy