現狀:html
當前問題:vue
// 測試使用: <div id="app"> <button @click="test">測試按鈕</button> </div> <script> var app = new Vue({ el: '#app', methods: { test: (() => { `use strict` console.log(this) // Window var flag = true return () => { console.log(this) // Window flag = false } })() } }) </script>
undefined
debugger
以後, 咱們一步步來看pointJump: (() => { let isFirstChanged = false; console.log(this); debugger; return entry => { console.log(this); console.log(isFirstChanged); debugger; isFirstChanged = true; }; })(),
操做:緩存
this
所表明的空間仍是undefined
, 因此沒有能改變過來.<script> var app = new Vue({ el: '#app', methods: { test: (() => { `use strict` console.log(this) // Window var flag = true return () => { console.log(this) // Window flag = false } })() } }) </script>
undefined
const test = (() => { let aaa = true; return function () { console.log(this); aaa = false; }; })(); mainJump(entry) { test.call(this); },
pointJump: (() => { let isFirstChanged = false; return function () { console.log(this); // Vue的頂級對象 isFirstChanged = true; }; })(),