请问以下JS代码最后输出的len值是多少?
var len = 117; let func = { len: 935, showLen: function() { console.log(this.len); }, show: function() { (function(cb) { cb(); })(this.showLen) } } func.show();
// 在JavaScript的函数中,this始终指向调用者的上下文环境 var len = 117 // 5. 全局作用域中使用 var 定义的变量默认会成为 window 的属性,及 window.len let func = { len: 935, showLen: function () { console.log(this.len) // 4. this 此时指向的是 window,所以相当于打印 window.len }, show: function () { (function (cb) { cb() // 3. cb 相当于 cb.call() 默认没有传入上下文环境时 this 指向全局的 window 对象 })(this.showLen) // 2. this 是 func 所以传入的是上面定义的 showLen 函数 } } func.show() // 1. 相当于 func.show.call(func),此时 this 是 func
var len = 117; let func = { len: 935, showLen: function() { console.log(this.len); }, show: function() { const f = function(cb) { cb(); } f(this.showLen) } } func.show();