63

Suppose if I define a class in file1.js

function Customer(){
    this.name="Jhon";
    this.getName=function(){
        return this.name;
    };
};

Now if I want to create a Customer object in file2.js

var customer=new Customer();
var name=customer.getName();

I am getting exception: Customer is undefined, not a constructor.

But when i create a customer object in file2.js and pass it to file1.js then its working .

file1.js

    function Customer(){
        this.name="Jhon";
        this.getName=function(){
            return this.name;
        }
    }
    function customer(){
        return new Customer();
    }

file2.js

    var customer=customer();
    var name=customer.getName();

but i want to create a customer object in file1.js using new Customer(). How can i achieve that?

0

7 Answers 7

48

It depends on what environment you're running in. In a web browser you simply need to make sure that file1.js is loaded before file2.js:

<script src="file1.js"></script>
<script src="file2.js"></script>

In node.js, the recommended way is to make file1 a module then you can load it with the require function:

require('path/to/file1.js');

It's also possible to use node's module style in HTML using the require.js library.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! I needed to break up my React file into multiple classes and I couldn't use the require because it's not supported in the web browser. I can just simple reference class component A from class component B without any require, import or export. I simply needed to load FileA before File B.
@Thuy React jsx compiler automatically splits your project into multiple small js files. Are you not using jsx?
I can't install nodejs in my project web resources, so I need to link all of the React files necessary in the HTML file. That way, I don't need the require when one React file refers to another file.
You only need node.js on your development PC/Mac/Laptop. You don't need it on your server. React compiles everything to static html and js files that you can then serve on anything
You need node.js to write react code. You don't need node.js to run react code. Think of node.js as the compiler for React projects.
28
// Create Customer class as follows:
export default class Customer {
   getName() {
     return 'stackoverflow';
   }
}

// Import the class 
// no need for .js extension in path cos it gets inferred automatically
import Customer from './path/to/Customer'; 
// OR
const Customer = require('./path/to/Customer') 

// Use the class
var customer = new Customer();
var name = customer.getName();

5 Comments

Although this solution works like a charm, this needs babel setup in the nodejs config. Learn't it the hard way!
@Krishna -- this could be my issue. When the class is defined in a separate file, instances passed between the files are not recognizing the class instances and allowing the methods. Do you have a link to the babel setup solution?
@user172431 npm install babel-cli babel-core --save-dev. Search [npmjs.com]
I'm getting two errors. One, SyntaxError: export declarations may only appear at top level of a module Two, ReferenceError: require is not defined
@AaronFranke, require above is for Node.js applications, but there is a require.js library file that you can add to your project [npmjs.com/package/require.js] or [npmjs.com/package/require.js].
9

You can export your methods to access from other files like this:

file1.js

var name = "Jhon";
exports.getName = function() {
  return name;
}

file2.js

var instance = require('./file1.js');
var name = instance.getName();

1 Comment

ReferenceError: require is not defined
5

If you are using javascript in HTML, you should include file1.js and file2.js inside your html:

<script src="path_to/file1.js"></script>
<script src="path_to/file2.js"></script>

Note, file1 should come first before file2.

Comments

4

Make sure the dom is loaded before you run your code in file2... If you're using jQuery:

$(function(){
  var customer=customer();
  var name=customer.getName();
});

Then it doesn't matter what order the files are in, the code won't run until all of the files are loaded.

Comments

2

Possible Suggestions to make it work:

Some modifications (U forgot to include a semicolon in the statement this.getName=function(){...} it should be this.getName=function(){...};)

function Customer(){
this.name="Jhon";
this.getName=function(){
return this.name;
};
}

(This might be one of the problem.)

and

Make sure U Link the JS files in the correct order

<script src="file1.js" type="text/javascript"></script>
<script src="file2.js" type="text/javascript"></script>

Comments

2

It is saying the value is undefined because it is a constructor function, not a class with a constructor. In order to use it, you would need to use Customer() or customer().

First, you need to load file1.js before file2.js, like slebetman said:

<script defer src="file1.js" type="module"></script>
<script defer src="file2.js" type="module"></script>

Then, you could change your file1.js as follows:

export default class Customer(){
    constructor(){
        this.name="Jhon";
        this.getName=function(){
            return this.name;
        };
    }
}

And the file2.js as follows:

import { Customer } from "./file1";
var customer=new Customer();

Please correct me if I am wrong.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.