javascript - "TypeError: string is not a function"的含义

标签 javascript node.js

错误“TypeError: string is not a function”到底是什么意思? 哪些条件会触发错误?

一些上下文来显示错误出现的位置。

下面的程序

"use strict;"
((console["log"])(42));

报错

/private/var/folders/k6/grq8nv093hj78x5m172d725m0000gn/T/tmp14388866091438886609658.js:2
((console["log"])(42));
^
TypeError: string is not a function
    at Object.<anonymous> (/private/var/folders/k6/grq8nv093hj78x5m172d725m0000gn/T/tmp14388866091438886609658.js:2:1)

    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

在 Node 中。但是 console["log"] 又名 console.log 应该是一个函数。

但是这个程序

"use strict;"
function displayln(v){return ((console["log"])(v));};
(displayln(42));

运行没有任何错误。

最佳答案

我认为它认为您正在尝试将“use strict;”作为函数执行。尝试在后面添加分号

"use strict";
((console["log"])(42));

关于javascript - "TypeError: string is not a function"的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31863629/

相关文章:

node.js - 如何在mongodb中查询嵌套的对象数组?

javascript - 在 promise API 调用 jest 中测试 then()

javascript - Rails PWA 中的 CacheStorage 始终为空。在 service-worker.js 中,cache.addAll 似乎没有缓存任何内容

javascript - 如何从js文件中的jquery函数调用HTML文件?

javascript - 在 HTML > JavaScript > WebForms 中嵌套引号或如何在 JavaScript 方法中调用 .NET 方法

javascript - 如何编写带有 M/D/YYYY 格式日期条件并存储为字符串的查找查询

node.js - 使用 json2csv 创建后如何下载 CSV 文件

javascript - 一个盒子一次变色而不是两个?

node.js - Webpack 未在 Linux 上安装

javascript - 使用 Javascript 对数据库进行用户注册和身份验证