Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 将本机原型扩展包含到节点JS文件中_Node.js - Fatal编程技术网

Node.js 将本机原型扩展包含到节点JS文件中

Node.js 将本机原型扩展包含到节点JS文件中,node.js,Node.js,在本例中,考虑以下两个文件: src\helper\number.js Number.prototype.pad = function(size) { var s = String(this); while (s.length < (size || 2)) {s = "0" + s;} return s; } /* (1).pad(3) // => "001" (10).pad(3) // => "010"

在本例中,考虑以下两个文件:

src\helper\number.js

Number.prototype.pad = function(size) {
  var s = String(this);
  while (s.length < (size || 2)) {s = "0" + s;}
  return s;
}

/*
(1).pad(3) // => "001"
(10).pad(3) // => "010"
(100).pad(3) // => "100"
*/
String.prototype.toInt = function () {
    return parseInt(this);
};

现在,我怎样才能让剩下的JS代码文件,比如说
src\app.JS
,识别并获取这些扩展方法呢?

我让它工作起来了。这就是我的
src\app.js
的样子:

// Include prototype extensions
require("./helper/number");
require("./helper/string");

console.log((10).pad(3));  // => "010"
console.log("10".toInt()); // => 10
关键是
需要
整个文件而不指定对象