Hello fellow programmers,
程序员大家好,
I am wondering today how to access objects from another external js file. I'm using this as a way to organize my code throughout multiple js files. Here's an example of what I'm trying to say.
我今天想知道如何从另一个外部js文件访问对象。我正在使用它作为一种在多个js文件中组织我的代码的方法。这是我想说的一个例子。
Imagine this as the code from an external js file:
想象一下这是来自外部js文件的代码:
$(function () {
function Person() {
this.name = "Bob";
}
})
And I want to access that object in another js file:
我想在另一个js文件中访问该对象:
$(function () {
var person = new Person;
alert(person.name);
})
Is there a way to do something like that? How would I need to position the html?
有没有办法做那样的事情?我怎么需要定位html?
3 个解决方案
#1
0
My first wonder is why you have your JS wrapped in a function like so. You can see here I've accessed "hello" from another script loaded after it's set - as it's set in the global space.
我的第一个奇迹是为什么你把JS包装在这样的函数中。你可以看到我在其设置后加载的另一个脚本中访问了“hello” - 因为它是在全局空间中设置的。
https://jsfiddle.net/sj7bp97c/
<script src="https://pastebin.mozilla.org/?dl=8907696">
</script>
<script src="https://pastebin.mozilla.org/?dl=8907697">
</script>
One script sets the value, the other prints it to console. Unless it's required for your Javascript to be surrounded by functions, I'm not sure why you're doing so.
一个脚本设置值,另一个脚本将其打印到控制台。除非你的Javascript被函数所包围,否则我不确定你为什么要这样做。
#2
0
Reveal the function to the global scope:
将功能显示到全局范围:
$(function () {
function Person() {
this.name = "Bob";
}
return {
Person: Person
}
})()
When revealed to the global scope, you access it from within another JavaScript file as soon as the script has initiated. So you'll want to include it after you included this one.
当向全局范围显示时,您可以在脚本启动后立即从另一个JavaScript文件中访问它。所以你想要在包含这个之后加入它。
#3
0
You just need to mention both of your js script files in head section of html.
你只需要在html的head部分提到你的两个js脚本文件。
Javascript will take care rest of the things. <head> <script src="First.js"></script> <script src="Second.js"></script> </head>
Javascript会处理剩下的事情。
#1
0
My first wonder is why you have your JS wrapped in a function like so. You can see here I've accessed "hello" from another script loaded after it's set - as it's set in the global space.
我的第一个奇迹是为什么你把JS包装在这样的函数中。你可以看到我在其设置后加载的另一个脚本中访问了“hello” - 因为它是在全局空间中设置的。
https://jsfiddle.net/sj7bp97c/
<script src="https://pastebin.mozilla.org/?dl=8907696">
</script>
<script src="https://pastebin.mozilla.org/?dl=8907697">
</script>
One script sets the value, the other prints it to console. Unless it's required for your Javascript to be surrounded by functions, I'm not sure why you're doing so.
一个脚本设置值,另一个脚本将其打印到控制台。除非你的Javascript被函数所包围,否则我不确定你为什么要这样做。
#2
0
Reveal the function to the global scope:
将功能显示到全局范围:
$(function () {
function Person() {
this.name = "Bob";
}
return {
Person: Person
}
})()
When revealed to the global scope, you access it from within another JavaScript file as soon as the script has initiated. So you'll want to include it after you included this one.
当向全局范围显示时,您可以在脚本启动后立即从另一个JavaScript文件中访问它。所以你想要在包含这个之后加入它。
#3
0
You just need to mention both of your js script files in head section of html.
你只需要在html的head部分提到你的两个js脚本文件。
Javascript will take care rest of the things. <head> <script src="First.js"></script> <script src="Second.js"></script> </head>
Javascript会处理剩下的事情。