我可以从其他文件中访问变量吗?

时间:2021-11-09 15:57:37

Is it possible to use a variable in a file called first.js inside another file called second.js?

是否可以在另一个名为second.js的文件中使用名为first.js的文件中的变量?

first.js contains a variable called colorcodes.

first.js包含一个名为colorcodes的变量。

7 个解决方案

#1


123  

As Fermin said, a variable in the global scope should be accessible to all scripts loaded after it is declared. You could also use a property of window or (in the global scope) this to get the same effect.

正如Fermin所说,全局范围中的变量应该可以被声明后加载的所有脚本访问。您还可以使用window的属性或(在全局范围内)来获得相同的效果。

// first.js
var colorCodes = {

  back  : "#fff",
  front : "#888",
  side  : "#369"

};

... in another file ...

...在另一个文件中......

// second.js
alert (colorCodes.back); // alerts `#fff`

... in your html file ...

...在你的html文件中......

<script type="text/javascript" src="first.js"></script> 
<script type="text/javascript" src="second.js"></script> 

#2


12  

This should work - define a global variable in firstfile and access it from secondfile:

这应该工作 - 在firstfile中定义一个全局变量并从secondfile访问它:

<script src="/firstfile.js"></script>
<script src="/secondfile.js"></script>

firstfile.js:

firstfile.js:

var colors = {
   text:'#000000',
   background:'#aaaaaa',
   something_else:'blue'
};

secondfile.js:

secondfile.js:

do_something_with(colors.background);

Note that the order in which you load the script files is significant for some browsers (IE6 for sure, maybe others)

请注意,加载脚本文件的顺序对于某些浏览器来说很重要(肯定是IE6,也许是其他浏览器)

#3


6  

I did like what answer above said but although, it didn't worked with me

我确实喜欢上面说的答案,但是,它并没有对我有用

because I was declaring these variables inside JQuery $( document ).ready()

因为我在JQuery $(document).ready()中声明了这些变量

so make sure you declare your variables inside the <script> tag not somewhere else

所以请确保在

#4


4  

If you store your colorcodes in a global variable you should be able to access it from either javascript file.

如果将颜色代码存储在全局变量中,则应该能够从javascript文件中访问它。

#5


3  

I came across amplify.js. It's really simple to use. To store a value, let's call it "myValue", you do:

我遇到了amplify.js。它使用起来非常简单。要存储一个值,我们称之为“myValue”,您可以:

amplify.store("myKey", "myValue")

And to access it, you do

要访问它,你可以

amplify.store("myKey")

#6


0  

I may be doing this a little differently. I'm not sure why I use this syntax, copied it from some book a long time ago. But each of my js files defines a variable. The first file, for no reason at all, is called R:

我可能会这样做有点不同。我不知道为什么我使用这种语法,很久以前就从一些书中复制了它。但是我的每个js文件都定义了一个变量。第一个文件,完全没有任何理由,叫做R:

    var R = 
    { 
        somevar: 0,
        othervar: -1,

        init: function() {
          ...
        } // end init function

        somefunction: function(somearg) {
          ...
        }  // end somefunction

        ...

    }; // end variable R definition


    $( window ).load(function() {
       R.init();
    })

And then if I have a big piece of code that I want to segregate, I put it in a separate file and a different variable name, but I can still reference the R variables and functions. I called the new one TD for no good reason at all:

然后,如果我有一大块我想要隔离的代码,我将它放在一个单独的文件和一个不同的变量名称中,但我仍然可以引用R变量和函数。我毫无理由地打电话给新的TD:

    var TD = 
    { 
        xvar: 0,
        yvar: -1,

        init: function() {
           ...
        } // end init function

        sepfunction: function() {
           ...
           R.somefunction(xvar);
           ...
        }  // end somefunction

        ...

    }; // end variable TD definition


    $( window ).load(function() {
       TD.init();
    })

You can see that where in the TD 'sepfunction' I call the R.somefunction. I realize this doesn't give any runtime efficiencies because both scripts to need to load, but it does help me keep my code organized.

您可以看到在TD'sepfunction'中我称之为R.somefunction。我意识到这不会给任何运行时效率,因为两个脚本都需要加载,但它确实帮助我保持我的代码组织。

#7


0  

You can export the variable from first file using export.

您可以使用export从第一个文件导出变量。

//first.js
const colorCode = {
    black: "#000",
    white: "#fff"
};
export { colorCode };

Then, import the variable in second file using import.

然后,使用import导入第二个文件中的变量。

//second.js
import { colorCode } from './first.js'

export - MDN

出口 - MDN

#1


123  

As Fermin said, a variable in the global scope should be accessible to all scripts loaded after it is declared. You could also use a property of window or (in the global scope) this to get the same effect.

正如Fermin所说,全局范围中的变量应该可以被声明后加载的所有脚本访问。您还可以使用window的属性或(在全局范围内)来获得相同的效果。

// first.js
var colorCodes = {

  back  : "#fff",
  front : "#888",
  side  : "#369"

};

... in another file ...

...在另一个文件中......

// second.js
alert (colorCodes.back); // alerts `#fff`

... in your html file ...

...在你的html文件中......

<script type="text/javascript" src="first.js"></script> 
<script type="text/javascript" src="second.js"></script> 

#2


12  

This should work - define a global variable in firstfile and access it from secondfile:

这应该工作 - 在firstfile中定义一个全局变量并从secondfile访问它:

<script src="/firstfile.js"></script>
<script src="/secondfile.js"></script>

firstfile.js:

firstfile.js:

var colors = {
   text:'#000000',
   background:'#aaaaaa',
   something_else:'blue'
};

secondfile.js:

secondfile.js:

do_something_with(colors.background);

Note that the order in which you load the script files is significant for some browsers (IE6 for sure, maybe others)

请注意,加载脚本文件的顺序对于某些浏览器来说很重要(肯定是IE6,也许是其他浏览器)

#3


6  

I did like what answer above said but although, it didn't worked with me

我确实喜欢上面说的答案,但是,它并没有对我有用

because I was declaring these variables inside JQuery $( document ).ready()

因为我在JQuery $(document).ready()中声明了这些变量

so make sure you declare your variables inside the <script> tag not somewhere else

所以请确保在

#4


4  

If you store your colorcodes in a global variable you should be able to access it from either javascript file.

如果将颜色代码存储在全局变量中,则应该能够从javascript文件中访问它。

#5


3  

I came across amplify.js. It's really simple to use. To store a value, let's call it "myValue", you do:

我遇到了amplify.js。它使用起来非常简单。要存储一个值,我们称之为“myValue”,您可以:

amplify.store("myKey", "myValue")

And to access it, you do

要访问它,你可以

amplify.store("myKey")

#6


0  

I may be doing this a little differently. I'm not sure why I use this syntax, copied it from some book a long time ago. But each of my js files defines a variable. The first file, for no reason at all, is called R:

我可能会这样做有点不同。我不知道为什么我使用这种语法,很久以前就从一些书中复制了它。但是我的每个js文件都定义了一个变量。第一个文件,完全没有任何理由,叫做R:

    var R = 
    { 
        somevar: 0,
        othervar: -1,

        init: function() {
          ...
        } // end init function

        somefunction: function(somearg) {
          ...
        }  // end somefunction

        ...

    }; // end variable R definition


    $( window ).load(function() {
       R.init();
    })

And then if I have a big piece of code that I want to segregate, I put it in a separate file and a different variable name, but I can still reference the R variables and functions. I called the new one TD for no good reason at all:

然后,如果我有一大块我想要隔离的代码,我将它放在一个单独的文件和一个不同的变量名称中,但我仍然可以引用R变量和函数。我毫无理由地打电话给新的TD:

    var TD = 
    { 
        xvar: 0,
        yvar: -1,

        init: function() {
           ...
        } // end init function

        sepfunction: function() {
           ...
           R.somefunction(xvar);
           ...
        }  // end somefunction

        ...

    }; // end variable TD definition


    $( window ).load(function() {
       TD.init();
    })

You can see that where in the TD 'sepfunction' I call the R.somefunction. I realize this doesn't give any runtime efficiencies because both scripts to need to load, but it does help me keep my code organized.

您可以看到在TD'sepfunction'中我称之为R.somefunction。我意识到这不会给任何运行时效率,因为两个脚本都需要加载,但它确实帮助我保持我的代码组织。

#7


0  

You can export the variable from first file using export.

您可以使用export从第一个文件导出变量。

//first.js
const colorCode = {
    black: "#000",
    white: "#fff"
};
export { colorCode };

Then, import the variable in second file using import.

然后,使用import导入第二个文件中的变量。

//second.js
import { colorCode } from './first.js'

export - MDN

出口 - MDN