Javascript命名空间 [英] Javascript Namespacing

查看:77
本文介绍了Javascript命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过将javascript分成不同的文件并使每个文件成为sub命名空间来使我的javascript更加模块化。

I wish to make my javascript more more modular by splitting it up into different files and giving each file a 'sub' namespace like so.

subA .js

if(typeof ex == "undefined") {
    var ex = {};
}

ex.subA = function() {
//all functions of subA here
}

同样适用于subB等。

And same for subB etc.

目前我有1个文件, ex。 js

var ex = (function() {
    //private vars/funcs
    return {
        //public vars/funcs
    }
})();

看起来我应该将大部分功能移到subA.js和subB.js但仍包括ex.js在开始时,之后是subA.js和subB.js。

It looks like I should move most of my functions to subA.js and subB.js but still include ex.js at the start, with subA.js and subB.js afterwards.

我有很多问题。


  1. 我是很难记住我是如何制作初始命名空间文件的,ex.js.看起来匿名函数最初会将所有内容设为私有,但我不记得为什么需要将它括在括号中然后立即执行(); 最后。

从q1开始,我的子文件应该与ex.js格式相同,即包含anon函数括号并立即执行?

Following on from q1, should my sub files be in the same format as ex.js, ie, have the anon function wrapped in parentheses and executed straight away?

看起来子文件只能访问 ex ,这是真的吗?如果是,我如何允许我的子文件访问私有函数?

It looks like the sub files will only have access to the public functions of ex, is this true? If it is, how can I allow my sub files access to the private functions as well?

在我的HTML文件中,在我的document.ready函数(jQuery)中),我应该将ex初始化为变量,还是可以通过
单独调用每个函数?

In my HTML file, in my document.ready function (jQuery), should I be initialising ex to a variable or can I call each function individually by going



$(document).ready(function() {
    ex.doSomething();
    ex.doSomethingElse();
}

两者之间是否存在差异?我认为当包含ex.js时,会立即创建一个全局变量ex(由于匿名函数被立即执行),所以我不需要在document.ready中重新定义它。

Is there a difference between the two? I think that when ex.js is included, a global variable ex is created straight away (due to the anonymous function being executed straight away), so I shouldn't need to redefine it in document.ready.


  1. subA.js中的第一个if语句是否与 var ex = ex || {}; 哪个更好?

你有什么js代码风格标准?使用?

What js code style standards do you use?

如果您仔细阅读了所有这些内容,那么您应该得到一个赞成,欢呼。

If you read through all this you already deserve an upvote, cheers.

推荐答案

1。 函数(){return {}} 是一个闭包,用于为您不想在其他任何地方访问的变量和函数添加隐私,但是在那个功能。函数(function(){})周围的括号创建一个函数表达式。它们被使用,因为当你尝试立即执行函数时,解析器会抱怨 function(){}()而不将任何参数传递给()

1. The function(){ return{}} is a closure to add "privacy" to the variables and functions you don't want to be accessible anywhere else but within the scope of that function. The parentheses around the function (function(){}) create a function expression. They are used, because the parser will complain when you try to execute a function immediately function(){}() without passing any argument to ().

2。如果你想让 subA 或任何其他扩展对象拥有自己的子函数,那么是的,你必须像那样声明它,但不是必然是一个clousure。

2. If you want subA or any other extending object to have its own subfunctions, then yes, you have to declare it like that, but not neccesarly a clousure though.

你可以做到

ex.subA = function(x, y){
    return x+y;
}

可以称为 var sum = ex.subA (2,3);

或者你可以做到

ex.subA = (function(){

    return {
        add: function(x, y){
            return x+y;
        },
        multiply: function(x, y){
            return x*y;
        }
    }
})();

然后称之为 var sum = ex.subA.add(2,3 ); var multiplication = ex.subA.multiply(2,3);

有很多方法可以做到这一点。

There are many ways to do this.

3。是的,它是真实的。您可以将私有函数公开。关闭将不允许任何其他方式。

3. Yes, it is true. You can make the private functions public. The closure won't allow any other way to do it.

4。如果您想要别名,是的,您可以将其分配给变量。但是,没有必要。它会像你描述的那样工作得很好。

4. If you would like to alias, yes you can assign it to a variable. However, there is no need to. It will work fine the way you are describing it.

阅读 基本JavaScript命名空间模式 ,以了解javascript命名空间中的一些基本原理和模式。

Read Essential JavaScript Namespacing Patterns to understand some fundamentals and patterns in javascript namespacing.


subA.js中的第一个if语句与var ex = ex ||有什么不同
{};哪个更好?

Is the first if statement in subA.js any different from var ex = ex || {}; Which is better?

首先,if语句应为 if(typeof ex =='undefined' ) typeof 返回一个字符串。没有必要检查 ex 是否为假。

First of, the if statement should be if(typeof ex == 'undefined'), the typeof returns a string. There isn't any need to check if ex is false.

是的,它是不同的。如果已定义变量 ex ,则if语句不会执行任何变量赋值。因此,if语句更好。

Yes, it is different. The if statement will not do any variable assignment if the variable ex is already defined. Therefore, the if statement is better.


你使用什么js代码样式标准?

What js code style standards do you use?

取决于项目的范围,但主要是使用辅助函数扩展的深层对象。

Depends on the scope of the project, but mostly deep object extending with a helper function.

这篇关于Javascript命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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