我想知道如何将“this”传递到外部模块(脚本.js).
索引.html
<script src='./script.js' defer></script>
<script>
var interval = setInterval(function () {
if (document.readyState === 'complete') {
clearInterval(interval);
(function (scope) {
console.log('this inside of function')
console.log(scope) //this prints 'global {frames: global, ..}
})(this) // <<<<< THIS works
}
}, 100);
</script>
脚本.js
(function (scope) {
console.log('this inside of script.js')
console.log(scope) /// this prints 'undefined'
})(this) // << THIS is undefined.