nodejs字符串格式包,没有方法格式

nodejs stringformat package, has no method format

本文关键字:格式 有方法 字符串 nodejs      更新时间:2023-09-26

我安装nodejs字符串格式pacakage,并运行它们的第一个示例:https://npmjs.org/package/stringformat

但我得到以下错误:

> npm install stringformat
> node
> var $ = require('stringformat')
> console.log($.format("Hello, {0}!", "World"))
TypeError: Object function () {
            // Call as a method
            return format.apply(arguments[0], Array.prototype.slice.call(arguments, 1))
        } has no method 'format'
    at repl:1:16
    at REPLServer.self.eval (repl.js:110:21)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.EventEmitter.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)
    at ReadStream.onkeypress (readline.js:99:10)
    at ReadStream.EventEmitter.emit (events.js:98:17)
    at emitKey (readline.js:1095:12)
> 

问题出在哪里?

它没有format()方法。

您可以将其用作函数:

 console.log($("Hello, {0}!", "World"))

或者通过启用String扩展:

 $.extendString();
 // here, you call format() on a String instance .
 console.log("Hello, {0}!".format("World"));