1

我安装了nodejs stringformat 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)
> 

可能是什么问题?

4

1 回答 1

3

它没有format()方法。

您可以将其用作函数:

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

或通过启用String扩展:

 $.extendString();
 // here, you call format() on a String instance .
 console.log("Hello, {0}!".format("World"));
于 2013-08-12T08:52:27.060 回答