Javascript关于函数的问题

发布于 2024-08-29 17:44:32 字数 692 浏览 6 评论 0原文

在我的主网页 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨小沫ゞ 2024-09-05 17:44:32

是的,这种做法很常见,因为 Javascript 函数可以放入其他文件中并拉入以这种方式工作的页面中。

Yes, this is done quite regularly as Javascript functions can be put into other files and pulled into pages that work this way.

记忆里有你的影子 2024-09-05 17:44:32

您的第二个脚本标记指定 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 (the mouseMove 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.

话少心凉 2024-09-05 17:44:32

根据您的评论,这就是 TJ 所说的 - 您需要将第二个脚本块变成这样的内容:

<script type="application/javascript" src="Resources/JavaScript/proj4js-combined.js"></script>
<script type="application/javascript">

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>

...但是您实际上应该移动内联代码块(第二个


编辑 1:您的编程背景是什么?如果它是 C# 或 Java 之类的东西,您将需要忘记您对这些的了解,并以完全不同的方式处理 Javascript。 Javascript 是一种解释语言,而不是编译语言;除此之外,这意味着声明函数的顺序很重要

当我说“函数声明”时,我的意思是任何看起来像这样的东西:

function myNewFunction()
{
   // anything else here
}

这告诉 Javascript 解释器一个名为 myNewFunction 的新函数,其主体由花括号中的任何内容组成。

这是我在声明函数之前使用函数时所说的示例。考虑以下代码块(与任何其他 Javascript 隔离,例如在外部 Javascript 文件中):

function foo() // this line declares the function called "foo"
{

}

function bar() // this line declares the function called "bar"
{
    foo(); // this line invokes the previously declared function called "foo"
}

这将按预期工作,因为 foo() 在调用之前已声明。然而,您想要做的(听起来)类似于:

function foo() // this line declares the function called "foo"
{
    bar(); // this line tries to invoke the function called "bar"
           // but it hasn't been declared yet! so it's undefined
}

function bar() // this line declares the function called "bar"
{

}

如果您要运行第二个 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:

<script type="application/javascript" src="Resources/JavaScript/proj4js-combined.js"></script>
<script type="application/javascript">

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>

...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:

function myNewFunction()
{
   // anything else here
}

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):

function foo() // this line declares the function called "foo"
{

}

function bar() // this line declares the function called "bar"
{
    foo(); // this line invokes the previously declared function called "foo"
}

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:

function foo() // this line declares the function called "foo"
{
    bar(); // this line tries to invoke the function called "bar"
           // but it hasn't been declared yet! so it's undefined
}

function bar() // this line declares the function called "bar"
{

}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文