久久久久久久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 服務器時的歡迎消息)
主站蜘蛛池模板: 日韩中文字幕在线观看视频 | 91免费入口 | 欧美成人激情 | 秋霞a级毛片在线看 | 深夜福利影院 | 日韩一 | 欧美精品 在线观看 | 久操伊人 | 欧美黑人又粗大 | 九九视频在线观看视频6 | 亚洲欧美日韩久久 | 欧美激情精品久久久久久变态 | 一区二区三区国产好的精 | 久久看看| 欧美日韩电影免费观看 | 能看的av | 999久久久久久久久6666 | 亚洲精品一区二区二区 | 久久久精品一区二区三区 | 国产亚洲精品美女久久久久久久久久 | 中文字幕精品一区二区三区精品 | 国产精品99久 | 国产精品欧美一区二区三区不卡 | 中文字字幕在线中文乱码范文 | 久草热播| 麻豆国产一区二区三区四区 | 精品1区2区| 91视视频在线观看入口直接观看 | 亚洲 欧美 精品 | 成人精品在线 | 色小姐综合网 | 国产精品福利在线观看 | 免费视频一区二区 | 久久精品亚洲国产 | 久久免费精品 | 中文字幕视频在线 | 日日夜夜精品视频 | 久久天天 | 91 在线| 中文在线一区二区 | 国产欧美在线 |