简体   繁体   English

JavaScript全局声明

[英]Javascript global declaration

I've just started out with javascript and came across this line in a file called global.js There is only one line in the file. 我刚开始使用javascript,并在名为global.js的文件中遇到了这一行。该文件中只有一行。 I'm not sure what does App do. 我不确定App会做什么。 Any ideas? 有任何想法吗?

Filename: globals.js 文件名:globals.js

//The Application 
App = {};

它创建了一个名称为App ...的对象。

I would assume that the idea behind the global.js file in your case is to include the file in your base html/template so that the variables in there can be accessed from anywhere in the app. 我假设您所用的global.js文件背后的想法是将该文件包含在基本html / template中,以便可以从应用程序中的任何位置访问其中的变量。

In some of our projects, we've had global files like this containing references to collections and "setting" variables, which is quite handy :) 在我们的某些项目中,我们有这样的全局文件,其中包含对集合的引用和“设置”变量,这非常方便:)

It is creating an object named App in the global namespace. 它正在全局名称空间中创建一个名为App的对象。 App has no functions or attributes (yet). 应用程式尚无功能或属性。

It (implicitly) declares a variable called App in a "global" context and assigns an empty object as its value. 它(隐式)在“全局”上下文中声明一个名为App的变量,并将一个空对象分配为其值。 Assuming this is used on a web page somewhere, it's the same as declaring window.App = {} . 假设此代码在某处的网页上使用,则与声明window.App = {}

How this affects the rest of your application, we won't know until you post more relevant code. 这将如何影响您的应用程序的其余部分,直到您发布更多相关的代码,我们才能知道。

This script just instantiate this empty Object. 该脚本只是实例化此空对象。 Probably, others scripts that ran along with this uses that object and depends that it is created there. 可能与此一起运行的其他脚本使用了该对象,并依赖于该对象在此创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM