使用Javascript的命名空间中的严格模式

使用Javascript的命名空间中的严格模式,javascript,namespaces,Javascript,Namespaces,最近,在读一本很好的书时,我发现了“use strict”pragma的用法 如果在全局范围内声明“严格使用”,则它似乎是一种不好的做法。建议在每个函数中直接使用严格模式,如下所示: // Non-strict code... (function(){ "use strict"; // Define your library strictly... })(); // Non-strict code... var contName = new function () { "use

最近,在读一本很好的书时,我发现了“use strict”pragma的用法

如果在全局范围内声明“严格使用”,则它似乎是一种不好的做法。建议在每个函数中直接使用严格模式,如下所示:

// Non-strict code...

(function(){
  "use strict";

  // Define your library strictly...
})();

// Non-strict code...
var contName = new function () {
"use strict";


this.tmp;   // public temp var
var tmp;    // private tmp, propagate thru all!


var f = this;   // to call contName.functions when this = html or other object 
var o = this;   // to create objects.

f.rnd = function rnd (range) {
    return Math.floor(Math.random() * range);
};


    function private1 () {     // can be called only inside of contName

     var x = tmp;   // can see all outer private vars
     this.tmp;    // !== outer this.tmp

    var y = this.rnd(x);    // will not work
    var y = f.rnd(x);    // work
    }


o.Pixel = function (x, y, z, col) {
    this.x = x || 0;
    this.y = y || 0;
    this.z = z || 0;
    this.color = col || 0;
};
是否可以将严格模式定义为整个命名空间,而不是在每个函数中定义它?如果是,我可以有一个或两个代码样本吗


谢谢。

严格模式适用于声明它的执行上下文和上下文包含的所有上下文(通过
eval
创建的上下文周围有一些Squidgeness,但避免使用
eval
和Squidgeness),因此通常您会使用模块模式将其应用于所有代码:

(function() {
    "use strict";

    function foo() {
    }

    function bar() {
    }

    // ...presumably code hooking up `foo` and `bar` to events
    // or otherwise using them -- or of course, you could export
    // them from the scope...
})();
在上述情况下,严格模式不仅适用于匿名函数,还适用于
foo
bar
。例如,该代码将“工作”(通过创建全局变量):

代码失败,出现
引用错误
(唯一的更改是
“使用严格”
):

…因为strict mode做的许多有用的事情之一就是摆脱隐式全局变量的恐惧

下面是另一个示例,其中我们导出了一个函数,该函数即使在从非严格代码调用时也以严格模式运行:

var MyModule;
MyModule = MyModule || {};
(function(mod) {
    "use strict";

    mod.doSomethingUseful = doSomethingUseful;
    function doSomethingUseful() {
        // ...
    }

})(MyModule);

“松散”代码可以调用总是在严格模式下运行的MyModule.DoSomethingUser。结果是,您可以对代码应用严格模式,而不需要使用您的代码的每个人都使用它。非常方便。

第一种方法是在
中加上“严格使用”pragma在.js文件的第一行。因此,将严格评估该文件中的所有内容。
这本身并没有什么错。迟早所有浏览器都会弃用“旧js代码”。唯一真正的缺点可能是,如果您使用它,可能会破坏旧代码

第二种方式可能是这样的对象:

// Non-strict code...

(function(){
  "use strict";

  // Define your library strictly...
})();

// Non-strict code...
var contName = new function () {
"use strict";


this.tmp;   // public temp var
var tmp;    // private tmp, propagate thru all!


var f = this;   // to call contName.functions when this = html or other object 
var o = this;   // to create objects.

f.rnd = function rnd (range) {
    return Math.floor(Math.random() * range);
};


    function private1 () {     // can be called only inside of contName

     var x = tmp;   // can see all outer private vars
     this.tmp;    // !== outer this.tmp

    var y = this.rnd(x);    // will not work
    var y = f.rnd(x);    // work
    }


o.Pixel = function (x, y, z, col) {
    this.x = x || 0;
    this.y = y || 0;
    this.z = z || 0;
    this.color = col || 0;
};
tmp=新的o像素(300300)

该代码允许您拥有“私有”变量。注意,f也是一个“私有”变量

用法很简单:

var rvar = contName.rnd(5);
var obj = new contName.Pixel(300, 300)

第三条路可能是T.J.Crowder的,第四条是Doug Crockford的。。。 这些只是模仿语言中尚未正式出现的东西的不同方式

var Module = (function() {
    "use strict";

    var f = function() {
        // do stuff
    }

    return {  // return your namespace
        "myModuleFunction": f
    }
})();

我认为这应该也行。

var contName=new function(){…
这个构造没有你认为的功能。OP指出他/她认为有理由不在全局范围内使用它。(我不知道这些是什么,因为我从来没有在全局范围内做过任何事情。)Crockford的名字是“道格拉斯”(或“道格”)。对,它创建了一个对象。使用您定义为表达式一部分的匿名函数中的
原型
。这是一种迂回的、误导性的创建对象的方法。有些人喜欢闭包,有些人不喜欢。有些人喜欢对象,有些人不喜欢:)使用{}将创建类似的单例对象,但没有专用变量。
new
与闭包或专用变量无关。感谢您的编辑,Matt!我经常这样做(字符串而不是严格的…:-)
var Module = (function() {
    "use strict";

    var f = function() {
        // do stuff
    }

    return {  // return your namespace
        "myModuleFunction": f
    }
})();