javascript 什么是 Ext.namespace,我们应该如何使用它们?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18152188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:53:36  来源:igfitidea点击:

What is Ext.namespace, how should we use them?

javascriptextjsnamespacesextjs4.2

提问by Brian

I came across to Ext.namespace()in the project that I am working on.
I looked in Sencha's websiteand the explanation was not very helpful.

我是在Ext.namespace()我正在从事的项目中遇到的。
我查看了Sencha 的网站,解释不是很有帮助。

This is what they are saying:

他们是这样说的:

Creates namespaces to be used for scoping variables and classes so that they are not global. Specifying the last node of a namespace implicitly creates all other nodes.

创建用于范围变量和类的命名空间,以便它们不是全局的。指定命名空间的最后一个节点会隐式创建所有其他节点。

Ext.namespace('Company', 'Company.data');

They also mention that Ext.ns('Company.data')is preferable.

他们还提到这Ext.ns('Company.data')是可取的。

I apologize if this question seems simple or dumb, but I really want to completely understand this concept. Thanks in advance

如果这个问题看起来简单或愚蠢,我深表歉意,但我真的很想完全理解这个概念。提前致谢

This is what is not very clear to me:

这是我不太清楚的地方:

  • If I have Ext.namespace('Company', 'Company.data')at the top of my JS page, does this mean that it carries all the other function name and variables (like a global scope)?
  • What exactly 'Company' and 'Company.data' stand for in Ext.namespace('Company', 'Company.data')?
  • Why new convention Ext.ns('Company.data')does not have 'Company' like in Ext.namespace?
  • What does this mean Specifying the last node of a namespace implicitly creates all other nodes?
  • When exactly this idea should be used?
  • 如果我Ext.namespace('Company', 'Company.data')的 JS 页面顶部有,这是否意味着它包含所有其他函数名称和变量(如全局范围)?
  • 'Company' 和 'Company.data' 到底代表Ext.namespace('Company', 'Company.data')什么?
  • 为什么新公约Ext.ns('Company.data')中没有“公司”之类的Ext.namespace
  • 这是什么意思Specifying the last node of a namespace implicitly creates all other nodes
  • 什么时候应该使用这个想法?

回答by George

First off, this is what Ext.ns('Company.data')is roughly equivalent too:

首先,这Ext.ns('Company.data')也是大致等效的:

if (!Company) var Company = {};
if (!Company.Data) Company.Data = {};

Basically, it is just a shortcut for defining deeply nested structures of objects. It is useful if your project structures this way; I've seen projects with a Java backend that duplicate the com.company.app.data.packagepackage names in JavaScript, for which Ext.nsis a nice shortcut.

基本上,它只是定义深度嵌套对象结构的捷径。如果您的项目以这种方式构建,这将很有用;我见过使用 Java 后端复制com.company.app.data.packageJavaScript 中的 包名的项目,这 Ext.ns是一个很好的快捷方式。



Addressing your questions point by point:

逐点解决您的问题:

  • If I have Ext.namespace('Company', 'Company.data')at the top of my JS page, does this mean that it carries all the other function name and variables (like a global scope)?
  • 如果我Ext.namespace('Company', 'Company.data')的 JS 页面顶部有,这是否意味着它包含所有其他函数名称和变量(如全局范围)?

No. You have to add to Company.Data, like Company.Data.newVal = 5;.

不,您必须添加到Company.Data,例如Company.Data.newVal = 5;

  • What exactly 'Company' and 'Company.data' stand for in Ext.namespace('Company', 'Company.data')?
  • 'Company' 和 'Company.data' 到底代表Ext.namespace('Company', 'Company.data')什么?

They are names, chosen by you, following your projects convention.

它们是您根据项目约定选择的名称。

  • Why new convention Ext.ns('Company.data')does not have 'Company' like in Ext.namespace?
  • 为什么新公约Ext.ns('Company.data')中没有“公司”之类的Ext.namespace

Because 'Company' is implied. That is, Ext.ns('Company', 'Company.data')is like:

因为“公司”是隐含的。也就是说,Ext.ns('Company', 'Company.data')就像:

if (!Company) var Company = {};
if (!Company) var Company = {};
if (!Company.Data) Company.Data = {};

This makes it easier to see why the first 'Company' is redundant.

这使得更容易看出为什么第一个“公司”是多余的。

  • What does this mean Specifying the last node of a namespace implicitly creates all other nodes?

  • When exactly this idea should be used?

  • 这是什么意思Specifying the last node of a namespace implicitly creates all other nodes

  • 什么时候应该使用这个想法?

I answered these two above.

我在上面回答了这两个。

回答by Towler

Ext.namespace is really only useful if you want to define your own classes without using Ext.define.

Ext.namespace 只有在你想定义自己的类而不使用 Ext.define 时才有用。

The example below requires the namespaces to be created ahead of time:

下面的示例要求提前创建命名空间:

Company.data.Form = Ext.extend(...); //this is the old way of defining classes

The standard way for creating ExtJS classes is:

创建 ExtJS 类的标准方法是:

Ext.define('Comany.data.Form', {...});

This method creates the namespaces for you automatically.

此方法会自动为您创建命名空间。