How to instantiate a javascript class in another js file?
JavascriptJavascript Problem Overview
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?
Javascript Solutions
Solution 1 - Javascript
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.
Solution 2 - Javascript
// 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();
Solution 3 - Javascript
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();
Solution 4 - Javascript
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
.
Solution 5 - Javascript
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.
Solution 6 - Javascript
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>
Solution 7 - Javascript
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.