1

I was trying to create a module for node.js and i noticed something. Example

function Example() {
     this.property = "something";

}

Example.prototype.run = function() {
     console.log('hello world')
}

module.exports = Example;

with this code it say's that there's no method run. I need it to declare like

Example.prototype.run = function run() {}

to work. Why is that happening ?

1
  • 3
    How are you trying to run it? It works perfectly well: var Example = require('./example.js'); new Example().run(); Commented Nov 20, 2014 at 1:28

2 Answers 2

3

This should work fine as long as you actually call the constructor and create an object which is how you've configured the Example code:

var Example = require("./example");
var item = new Example();
item.run();
Sign up to request clarification or add additional context in comments.

Comments

0

You need to load the module and instantiate the Example class.

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();

run:

$ node main.js
hello world

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.