Javascript 如何在另一个js文件中实例化一个javascript类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12812757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to instantiate a javascript class in another js file?
提问by Tarak
Suppose if I define a class in file1.js
假设我在 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
现在,如果我想在 file2.js 中创建一个 Customer 对象
var customer=new Customer();
var name=customer.getName();
I am getting exception: Customer is undefined, not a constructor.
我收到异常: 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 .
但是当我在 file2.js 中创建一个客户对象并将其传递给 file1.js 然后它的工作。
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?
但我想使用 new Customer() 在 file1.js 中创建一个客户对象。我怎样才能做到这一点?
回答by slebetman
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
:
这取决于您运行的环境。在 Web 浏览器中,您只需要确保file1.js
在之前加载file2.js
:
<script src="file1.js"></script>
<script src="file2.js"></script>
In node.js, the recommended way is to make file1 a modulethen you can load it with the require
function:
在 node.js 中,推荐的方法是让 file1 成为一个模块,然后你可以使用require
函数加载它:
require('path/to/file1.js');
It's also possible to use node's module style in HTML using the require.jslibrary.
也可以使用require.js库在 HTML 中使用节点的模块样式。
回答by Camilo Ortegón
You can export your methods to access from other files like this:
您可以导出您的方法以从其他文件访问,如下所示:
file1.js
文件1.js
var name = "Jhon";
exports.getName = function() {
return name;
}
file2.js
文件2.js
var instance = require('./file1.js');
var name = instance.getName();
回答by ColinWa
// Create Customer class as follows:
export default class Customer {}
// Import the class
// no need for .js extension in path cos 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();
回答by Thom Porter
Make sure the dom is loaded before you run your code in file2... If you're using jQuery:
在 file2 中运行代码之前,请确保已加载 dom... 如果您使用的是 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.
那么无论文件的顺序如何,代码都不会运行,直到所有文件都加载完毕。
回答by Mifeng
If you are using javascript in HTML, you should include file1.js
and file2.js
inside your html:
如果你在 HTML 中使用 javascript,你应该在你的 html 中包含file1.js
和file2.js
:
<script src="path_to/file1.js"></script>
<script src="path_to/file2.js"></script>
Note, file1
should come first before file2
.
注意,file1
应该在file2
.
回答by Nokia808Freak
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(){...};
)
一些修改(你忘了在this.getName=function(){...}
应该是的语句中包含一个分号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
确保以正确的顺序链接 JS 文件
<script src="file1.js" type="text/javascript"></script>
<script src="file2.js" type="text/javascript"></script>
回答by yaya
For me, It was kind of stupid.
对我来说,这有点愚蠢。
i had syntax error in first file, so second file couldn't load it.
我在第一个文件中有语法错误,所以第二个文件无法加载它。