Javascript关于函数的问题
在我的主网页 (Viewer.aspx) 中,我有一个像这样的 javascript 脚本标签
<script language="javascript" type="text/javascript">
function initialize() {
var map = $find('Map1');
map.add_mouseMove(mouseMove);
}
</script>
在这些脚本标签中,我有一个函数。是否可以像这样调用不同脚本标记中的另一个函数?
<script language="javascript" type="text/javascript" src="Resources/JavaScript/proj4js-combined.js">
function mouseMove(sender,eventArgs) {
var source = new Proj4js.Proj('EPSG:3116');
var dest = new Proj4js.Proj('WGS84');
var p = new Proj4js.Point(px, py);
Proj4js.transform(source, dest, p);
}
</script>
In my main Web Page (Viewer.aspx) I have a javascript script tag like this
<script language="javascript" type="text/javascript">
function initialize() {
var map = $find('Map1');
map.add_mouseMove(mouseMove);
}
</script>
Within those script tags I have a function. Is it possible to call another function that is in a different script tag like this?
<script language="javascript" type="text/javascript" src="Resources/JavaScript/proj4js-combined.js">
function mouseMove(sender,eventArgs) {
var source = new Proj4js.Proj('EPSG:3116');
var dest = new Proj4js.Proj('WGS84');
var p = new Proj4js.Point(px, py);
Proj4js.transform(source, dest, p);
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这种做法很常见,因为 Javascript 函数可以放入其他文件中并拉入以这种方式工作的页面中。
Yes, this is done quite regularly as Javascript functions can be put into other files and pulled into pages that work this way.
您的第二个脚本标记指定
src
。 .js 文件的内容将被加载和解析,但 script 标记内的代码(mouseMove
函数)将被忽略。如果您想要 .js 文件的函数和内容,则需要将它们分成两个不同的脚本标签。Your second script tag specifies a
src
. The contents of the .js file will be loaded and parsed, but the code inside the script tag (themouseMove
function) will be ignored. If you want both the function and the contents of the .js file, you need to separate them into two different script tags.根据您的评论,这就是 TJ 所说的 - 您需要将第二个脚本块变成这样的内容:
...但是您实际上应该移动内联代码块(第二个
编辑 1:您的编程背景是什么?如果它是 C# 或 Java 之类的东西,您将需要忘记您对这些的了解,并以完全不同的方式处理 Javascript。 Javascript 是一种解释语言,而不是编译语言;除此之外,这意味着声明函数的顺序很重要。
当我说“函数声明”时,我的意思是任何看起来像这样的东西:
这告诉 Javascript 解释器一个名为 myNewFunction 的新函数,其主体由花括号中的任何内容组成。
这是我在声明函数之前使用函数时所说的示例。考虑以下代码块(与任何其他 Javascript 隔离,例如在外部 Javascript 文件中):
这将按预期工作,因为
foo()
在调用之前已声明。然而,您想要做的(听起来)类似于:如果您要运行第二个 Javascript 片段,它将不起作用,因为您在声明函数之前调用了函数。*
*脚注:实际情况并非如此,因为使用这种语法 (
function foo() { ... }
) 在 Javascript 中做了一些特殊而神奇的事情。我的特定示例实际上可以工作,但它说明了您遇到的问题。As per your comment, here's what T.J. was talking about - you need to turn your second script block into something like this:
...but you should really just move the inline code block (what's inside of the 2nd
<script>
tag in my answer) to an external Javascript file.Edit 1: What's the programming background you're coming from? If it's something like C# or Java, you'll need to forget what you know about those and approach Javascript completely differently. Javascript is an interpreted language, not a compiled one; among other things, this means that the order in which your functions are declared matters.
When I say "function declaration," I mean anything that looks like this:
This tells the Javascript interpreter about a new function, called
myNewFunction
, whose body consists of whatever is in the curly braces.Here's an example of what I'm talking about when I say you are using a function before you've declared it. Consider the following block of code (in isolation from any other Javascript, say, in an external Javascript file):
This will work as expected, because
foo()
was declared before you called it. However, what (it sounds like) you're trying to do is analogous to this:If you were to run this second Javascript snippet, it wouldn't work, because you're calling a function before it was declared.*
*Footnote: this is not actually the case, because using this syntax (
function foo() { ... }
) does something special and magical in Javascript. My particular example will actually work, but it illustrates the problem you're having.