JavaScript:两个单独的脚本 - 共享变量? [英] JavaScript: Two separate scripts - share variables?

查看:99
本文介绍了JavaScript:两个单独的脚本 - 共享变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在HTML页面中有两个单独的脚本,JavaScript是整个页面之间共享的变量?

 < / b>脚本> var title =Hello World!; < / script> 
//随机HTML / PHP
< script> document.write(title); < / script>

会写Hello World!
这似乎是一个坏的编码惯例我怎么能实现这样的正确的形式。

解决方案

您的示例被声明为全局变量,因此它将可用于加载到同一页面的任何和所有脚本。更重要的是,如果在同一个页面上已经有一个名为 title 的全局变量,那么当它赋值为Hello World!时,它的值将被覆盖。



避免这种问题的通常做法是声明一个全局变量,然后将所有其他变量放入其中。例如:

  var bobbyS_vars = {
title:Hello World!
};

为某个全局变量分配一个没有其他人可能选择的名称,例如您的姓名或



另一个更常见的处理这个问题的方法是利用自己的名字或者最好的JavaScript处理函数中的变量范围的方式。例如,创建一个匿名函数,在该函数中声明您的代码的全部 ,然后通过在声明的末尾加上()来调用声明结束处的函数。例如:

 (function(){
var title =Hello World!;

document.write(title);
})();

// title不在此处,因此它是未定义的,
//除非在别处声明。

如果您想要共享某些变量,匿名函数使用以下方法的组合:

  var bobbyS_vars = {
title:Hello World!
};

(function(){
var employeeId =E 298;
var count = 7;

document.write(&p ;+ bobbyS_vars.title +< / p>);
document.write(< p>+ employeeId +< / p>);
}) ;

//此时,bobbyS_var.title在范围内,仍然有
//值Hello World!。变量employeeId和count在范围内不是
//,并且对上面的代码是有效的。

最后一个注意事项。你的代码声明的所有函数也是有效的全局变量。所以,如果你创建一个名为printTitle的函数,它是1)可用于页面上的所有其他代码,2)可以覆盖或被同一页上的另一个函数覆盖,也称为printTitle。您可以使用与任何其他变量相同的方式保护和/或公开您的函数:

  var bobbyS_vars = {}; 

(function(){
//私有函数
var function = addOne(i){
return i + 1;
};

// Public vars
bobbyS_vars.title:Hello World!;

//公共函数
bobbyS_vars.printTitle = function(){
document.write(< p>+ bobbyS_vars.title +< / p>);
document.write(< p>+ addOne(41)+ p>);
};
})();

//此时,函数addOne不能直接访问,
//但printTitle是。
bobbyS_vars.printTitle();请注意,尽管函数addOne实际上是闭包内的私有函数,但它仍然可以间接地访问,通过() printTitle函数,因为addOne和printTitle都在同一范围内。


If I have two separate scripts in an HTML page with JavaScript are the variables shared between the entire page? Or only within their own declarations?

Example:

<script> var title = "Hello World!"; </script>
// random HTML/PHP
<script> document.write(title); </script>

Will that write "Hello World!"? This seems like bad coding convention how else could I achieve something like this with proper form.

解决方案

Variable title in your example is declared as a global variable, therefore it will be available to any and all scripts loaded into the same page. Whats more, if there is already a global variable named title on the same page, its value will be overwritten when you assign it the value "Hello World!"

The usual practice to avoid this sort of problem is to declare exactly one global variable, then put all of your other variables inside it. For example:

var bobbyS_vars = {
    title: "Hello World!";
};

Assign that lone global variable a name that no one else is likely to choose, such as your name or employer's name or best-of-all, a domain name that belongs you or your employer.

Another, more common way to handle this problem is to take advantage of of the way that JavaScript handles variable scope within functions. For example, create an anonymous function, declare all of your code inside that function, then call the function at the end of the declaration by putting () at the end of the declaration. For example:

(function() {
    var title = "Hello World!";

    document.write(title);
})();

// title is not in scope here, so it is undefined,
// unless it were declared elsewhere.

If you want to share some variables, but not others, have your anonymous function use a combination of approaches:

var bobbyS_vars = {
    title: "Hello World!";
};

(function() {
    var employeeId = "E 298";
    var count = 7;

    document.write("<p>" + bobbyS_vars.title + "</p>");
    document.write("<p>" + employeeId + "</p>");
})();

// At this point, bobbyS_var.title is in scope and still has the 
// value "Hello World!". Variables employeeId and count are not
// in scope and effectively private to the code above.

One final note. All of the functions that your code declares are also effectively global variables. So, if you create a function named printTitle, it is 1) available to all other code on the page and 2) could overwrite or be overwritten by another function on the same page also named printTitle. You can protect and/or expose your functions the same way you would any other variable:

var bobbyS_vars = { };

(function() {
    // Private functions
    var function = addOne(i) {
        return i + 1;
    };

    // Public vars
    bobbyS_vars.title: "Hello World!";

    // Public functions
    bobbyS_vars.printTitle = function() {
        document.write("<p>" + bobbyS_vars.title + "</p>");
        document.write("<p>" + addOne(41) + "</p>");
    };
})();

// At this point, function addOne is not directly accessible,
// but printTitle is.
bobbyS_vars.printTitle();

Note that although function addOne is effectively a private function within the closure, it is still accessible indirectly, via the printTitle function because addOne and printTitle are both within the same scope.

这篇关于JavaScript:两个单独的脚本 - 共享变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆