久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

未捕獲的類型錯誤:this.method 不是函數 - 節點 js

Uncaught TypeError: this.method is not a function - Node js class export(未捕獲的類型錯誤:this.method 不是函數 - 節點 js 類導出)
本文介紹了未捕獲的類型錯誤:this.method 不是函數 - 節點 js 類導出的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我是 node.js 的新手,我正在嘗試要求一個類.我用過 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes 作為參考.但是,例如,當我這樣做時:

I am new to node.js and I am trying to require a class. I have used https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes as reference. However, when I do this for example:

// talker.js
class Talker {
    talk(msg) {
        console.log(this.say(msg))
        var t = setTimeout(this.talk, 5000, 'hello again');
    }
    say(msg) {
        return msg
    }
}
export default Talker

// app.js
import Talker from './taker.js'
const talker = new Talker()
talker.talk('hello')

我明白了:

talker.js:4 Uncaught TypeError: this.say 不是函數

talker.js:4 Uncaught TypeError: this.say is not a function

應該說app.js是electron.js的渲染進程,使用rollup.js捆綁

It should be said that app.js is the electron.js renderer process and it bundled using rollup.js

任何想法為什么會這樣?

Any ideas why this would be?

更新:抱歉,我在輸入偽代碼時忘記添加一行.當我用回調調用 setTimeout 時,它實際上會發生.我已經更新了代碼.

Update: Sorry, I forgot to add in a line when putting in the psuedo code. It actually happens when I call setTimeout with callback. I have updated the code.

推薦答案

你正在失去 this 對你的方法的綁定.

You are losing the bind of this to your method.

從此改變:

setTimeout(this.talk, 5000, 'hello again');

到這里:

setTimeout(this.talk.bind(this), 5000, 'hello again');

<小時>

當您將 this.talk 作為函數參數傳遞時,它接受 this 并查找方法 talk 并傳遞對該方法的引用功能.但是,它只傳遞對該函數的引用.與您在 this 中的對象不再有任何關聯..bind() 允許您將引用傳遞給一個小存根函數,該函數將跟蹤 this 并將您的方法稱為 this.say(),而不僅僅是 say().


When you pass this.talk as a function argument, it takes this and looks up the method talk and passes a reference to that function. But, it only passes a reference to that function. There is no longer any association with the object you had in this. .bind() allows you to pass a reference to a tiny stub function that will keep track of this and call your method as this.say(), not just as say().

如果你這樣做,你會看到同樣的事情:

You can see the same thing if you just did this:

const talker = new Talker();'

const fn = talker.say;
fn();

這會產生同樣的問題,因為將方法分配給 fn 根本不會與 talker 關聯.它只是一個函數引用,與對象沒有任何關聯.事實上:

This would generate the same issue because assigning the method to fn takes no associate to talker with it at all. It's just a function reference without any association with an object. In fact:

talker.say === Talker.prototype.say

.bind() 所做的是創建一個小的存根函數,該函數將保存對象值,然后使用該對象調用您的方法.

What .bind() does is create a small stub function that will save the object value and will then call your method using that object.

這篇關于未捕獲的類型錯誤:this.method 不是函數 - 節點 js 類導出的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 麻豆久久久久久久 | 国产精品成人久久久久a级 久久蜜桃av一区二区天堂 | 久久a久久 | 99福利视频 | 国产高清视频一区 | 在线国产一区 | 91精品国产91久久综合桃花 | 国产成人精品综合 | 精品国产一区二区三区久久久蜜月 | 99热在线观看精品 | 黄网在线观看 | 中文字幕二区 | 在线欧美视频 | 亚洲字幕在线观看 | 草草视频在线播放 | 色婷婷av久久久久久久 | 狠狠综合久久av一区二区小说 | 91精品国产综合久久婷婷香蕉 | 嫩草国产 | 国产丝袜av | 一二三四在线视频观看社区 | 日韩视频观看 | 日韩欧美在线一区二区 | 性国产xxxx乳高跟 | 精品国产18久久久久久二百 | 亚洲夜夜爽 | 国产成人高清成人av片在线看 | 亚洲国产精品视频一区 | 日日天天 | 伊人天堂网 | 日韩中文字幕免费 | 欧美一级淫片免费视频黄 | 日韩精品在线一区 | 日韩精品免费视频 | 亚洲第一色站 | 久久久成人精品 | 欧美一区二区在线 | 欧美在线a | 日韩在线精品 | 一区二区三区中文字幕 | 亚洲444eee在线观看 |