nodejs stringformat包,没有方法格式

时间:2013-08-12 08:48:05

标签: javascript node.js

我安装了节点js字符串格式包,并运行了他们的第一个例子: 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)
> 

可能是什么问题?

1 个答案:

答案 0 :(得分:3)

它没有format()方法。

您可以将其用作功能:

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

或启用String扩展程序:

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