如何在“模块”中组织我的代码?

发布于 2024-10-21 17:47:06 字数 1474 浏览 1 评论 0原文

我正在努力组织我的代码。我的项目中有几个模块,我想对其进行组织。

关键是我脑子里想到的一切都行不通。我目前正在考虑四个想法:

  1. 简单对象 - 由于范围问题没有用。使用 this.a 可以,但是 this 根据调用者的不同而具有不同的含义,因此它不可靠。例如,我曾经将一个函数分配给 WebSocket 类,但在调用该函数时突然 this 引用了 WebSocket 实例通过 WebSocket 事件。我可以在每次调用该函数时使用 bind(foo),但我猜一定还有另一种方法。

    var foo = {
        答:3,
        s: 函数() {
            警报(一); // a 在此范围内不存在
            警报(这个。a); // 'this' 并不总是 foo
            警报(foo.a); // 我必须输入“foo”。每个变量之前
                          // 参考,但我确信这不是这样做的方法
       }
    };
    
  2. 实例 - 未定义a。再次强调,这个并不可靠。

    var foo = function() {
        这个.a = 3;
        this.s = 函数() {
            警报(一);
        };
    };
    var foo_instance = new foo();
    foo_instance.a = 4;
    foo_instance.s(); // 错误:a 未定义
    
  3. 实例关闭 - 不返回任何内容;它保持未定义

    var foo = (function() {
        这个.a = 3;
        this.s = 函数() {
            警报(一);
        };
    })();
    // foo === 未定义
    
  4. 使用 getter/setter 闭包 - 在 Chrome 上运行良好,但 IE 不支持 getter/setter。

    var foo = (function() {
        var a = 3;
        返回 {
            获取 a() { 返回 a; },
            设置 a(v) { a = v; },
    
            s: 函数() {
                警报(一); // 在 IE 中不像 getter/setter 那样工作
                          // 不支持
            }
        };
    })();
    

我如何有效地组织我的模块,以便我可以安全地以跨浏览器的方式访问属性?

谢谢。

I'm trying to wrap my head around organising my code. I have several modules within my project, which I'd like to organise.

The point is that all what has come to my mind doesn't work out. I'm currently thinking of four ideas:

  1. Simple object - Is not useful due to scoping issues. Using this.a would work, but this has a different meaning depending on who called it so it's not reliable. For example, I once assigned a function to a WebSocket class, but all of a sudden this referred to the WebSocket instance when the function was called by a WebSocket event. I could use bind(foo) each time I call the function, but there must be another way I guess.

    var foo = {
        a: 3,
        s: function() {
            alert(a); // a doesn't exist in this scope
            alert(this.a); // 'this' isn't always foo
            alert(foo.a); // I would have to put 'foo.' before each variable
                          // reference, but I'm sure that's not the way to do it
       }
    };
    
  2. Instance - a is not defined. Again, this isn't reliable.

    var foo = function() {
        this.a = 3;
        this.s = function() {
            alert(a);
        };
    };
    var foo_instance = new foo();
    foo_instance.a = 4;
    foo_instance.s(); // Error: a is not defined
    
  3. Closure with instance - Doesn't return anything; it stays undefined.

    var foo = (function() {
        this.a = 3;
        this.s = function() {
            alert(a);
        };
    })();
    // foo === undefined
    
  4. Closure with getter/setter - Works beautifully on Chrome, however IE doesn't support getters/setters.

    var foo = (function() {
        var a = 3;
        return {
            get a() { return a; },
            set a(v) { a = v; },
    
            s: function() {
                alert(a); // Doesn't work in IE as getters/setters are
                          // not supported
            }
        };
    })();
    

How would I effectively organise my modules, so that I can access the properties safely and in a cross-browser way?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Smile简单爱 2024-10-28 17:47:06

3 未定义,因为您没有返回任何内容。不要将属性和方法分配给“this”,而是尝试这样做:

var foo = (function() {
    var self = {};

    self.someProperty = someValue;

    self.someFunction = function () {

    }

    return self;
}());

foo 现在将返回一个具有定义的属性和方法的对象。这样做你永远不必想知道“this”实际上指的是什么。

3 is undefined because you are not returning anything. instead of assigning properties and methods to 'this', try this:

var foo = (function() {
    var self = {};

    self.someProperty = someValue;

    self.someFunction = function () {

    }

    return self;
}());

foo will now return an object with the properties and methods defined. doing it this way you never have to wonder what 'this' is actually referring to.

王权女流氓 2024-10-28 17:47:06

在我看来,你并没有真正理解 this 和闭包在 JavaScript 中的工作原理。

请阅读 两者这些 主题,还可以查看 命名空间

实现模块的方法有很多种,但是除非您了解基础知识,否则在这里讨论它没有多大意义,因此请参阅我的链接以获取深入的解释。

It seems to me that you have no real understand of how this and closures work in JavaScript.

Please read up on both of these topics and also have a look at namespaces.

There are a ton of different ways how one could realize modules, but it doesn't make much sense to talk about it here unless you understand the basics, so please refer to my links for a in-depth explanation.

℡Ms空城旧梦 2024-10-28 17:47:06

您的第一个代码片段使用闭包,并且对应于 yui 库 流行的模式。第二种模式对应于对象的私有、公共和特权成员的概念。

我建议您阅读 Douglas Crockford 撰写的关于 javascript 私有成员 的主要文章,然后选择第一个选项或第二个选项。 它们在语义上是等效的

(与前两个相比,第三个和第四个片段对我来说似乎过于复杂)

Your first code snippet uses a closure, and corresponds to a pattern that was made popular by the yui library. The second pattern corresponds to the notion of private, public and privileged members of an object.

I recommend that you read this staple article about javascript private members by Douglas Crockford, and go either with the first option or the second. They are semantically equivalent.

(The third and the forth snippets seem overly complex to me in comparison to the first two)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文