有什么方法可以覆盖闭包,使其完成原始闭包的部分功能吗?我知道没有直接的方法,但是有一些技巧吗?我愿意凌乱...
<html>
<head>
// I DON'T CONTROL THIS CODE!!!
<script>
;(function() {
function _dothing() {
alert("_dothing");
}
function _doit() {
_dothing();
alert("_doit");
}
window.K = { doit : _doit };
})();
</script>
</head>
<body>
// I DO CONTROL THIS CODE
<script>
function mydoit() {
alert("mydoit");
_dothing(); <-- THIS FAILS, IS THERE ANY WAY TO SUCCEED? :(
}
window.K.doit = mydoit;
window.K.doit();
</script>
</body>
</html>
最佳答案
我认为您可以使用 jQuery 获取脚本标记的内容,然后您可以使用 eval
参见 question .
关于javascript:在闭包范围内调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12960963/