亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 javascript 類中調用類方法

在 javascript 類中調用類方法

翻閱古今 2022-01-01 21:08:10
這是一個 Vue 類。該方法signOut()應在計時器滴答時觸發。計時器工作,除了 call signOut()。問題在于訪問類方法。我對this, self 和 access 修飾符感到困惑。我試過了,this.signOut()但它不起作用。我如何調用該方法signOut?"use strict";(async (globals, config, loader, application) => {    const storageLocal = await loader.services.storage.local.getAsync();    class HeaderComponent {        #foo = a;        constructor(tag) {            this.tag = tag;            this.timer();        }        signOut() {            storageLocal.delete('account');            window.location = '/signin.html';        }        timer() {            //document.getElementById("timer"),             var counter = -1;            var timeout;            var startTimer = function timer() {                counter++;                console.log(counter);                signOut(); //<- error can't call class method                timeout = setTimeout(timer, 10000);            };            function resetTimer() {                // here you reset the timer...                clearTimeout(timeout);                counter = -1;                startTimer();                //... and also you could start again some other action            }            document.addEventListener("mousemove", resetTimer);            document.addEventListener("keypress", resetTimer);            startTimer();        }        data() {            return { account: storageLocal.account };        }    }    const component = new HeaderComponent('component-header')    loader.components.set(component.tag, component);})(window, window.config, window.loader, window.application);請注意:        signOut() {            storageLocal.delete('account');            window.location = '/signin.html';        }        timer() {            //document.getElementById("timer"),             var counter = -1;            var timeout;            var startTimer = function timer() {如您所見,“signOut()”比活動函數低 2 個級別。邏輯說它會工作,this.parent.signOut()但它不會!
查看完整描述

3 回答

?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

將function創建一個新的上下文。您需要切換到箭頭功能并使用this.signOut(). 簡化示例:


  timer() {

    var counter = -1;

    var timeout;

    var startTimer = () => {

      counter++;

      console.log(counter);


      this.signOut();

      timeout = setTimeout(startTimer, 1000);

    };


    setTimeout(startTimer, 1000);

  }

此外,您signOut()在一個類中定義了兩個方法。


查看完整回答
反對 回復 2022-01-01
?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

你需要this并稱之為this.signOut()


查看完整回答
反對 回復 2022-01-01
?
慕哥9229398

TA貢獻1877條經驗 獲得超6個贊

該startTimer功能全不中的上下文中運行HeaderComponent的實例。 thisinstartTimer將指向window它作為 in 中的處理程序執行的時間setTimeout。


為了訪問 的實例HeaderComponent,要么使用箭頭函數(如之前的答案中所指出的那樣。另見箭頭函數表達式),它將指向this外部上下文(即HeaderComponent的實例)或定義一個標識符,timer其中指向到實例(例如const self = this;)并使用self而不是thisin startTimer。


將此應用于您的示例(為了保持一致性,我使用了var代替const):


        timer() {

            var counter = -1;

            var timeout;

            var self = this;

            var startTimer = function() { // Don't use a named function here, it only leads to more confusion

                counter++;

                console.log(counter);


                self.signOut(); // Use `this` of the outer context

                timeout = setTimeout(startTimer, 10000);  // Use the declared identifier

            };


            // Rest of the method

        }

this對于那些來自不同編程語言的人來說,Javascript 可能有點混亂。如果您想了解更多細節,我建議您閱讀MDN 參考資料this和Closures


查看完整回答
反對 回復 2022-01-01
  • 3 回答
  • 0 關注
  • 703 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號