![](/img/trans.png)
[英]Difference between two types of function declaration in javascript inside a class?
[英]Difference between javascript class declaration
我试图为node.js创建一个模块,我发现了一些东西。 例
function Example() {
this.property = "something";
}
Example.prototype.run = function() {
console.log('hello world')
}
module.exports = Example;
用这段代码说它没有方法运行。 我需要它来宣布
Example.prototype.run = function run() {}
上班。 为什么会这样?
只要您实际调用构造函数并创建一个对象,您应该如何配置示例代码,这应该可以正常工作:
var Example = require("./example");
var item = new Example();
item.run();
您需要加载模块并实例化 Example类。
Example.js:
function Example() {
this.property = "something";
}
Example.prototype.run = function() {
console.log('hello world')
}
module.exports = Example;
main.js:
var Example = require("./Example.js");
var example = new Example();
example.run();
跑:
$ node main.js
hello world
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.