如何在“模块”中组织我的代码?
我正在努力组织我的代码。我的项目中有几个模块,我想对其进行组织。
关键是我脑子里想到的一切都行不通。我目前正在考虑四个想法:
简单对象 - 由于范围问题没有用。使用
this.a
可以,但是this
根据调用者的不同而具有不同的含义,因此它不可靠。例如,我曾经将一个函数分配给WebSocket
类,但在调用该函数时突然this
引用了WebSocket
实例通过WebSocket
事件。我可以在每次调用该函数时使用bind(foo)
,但我猜一定还有另一种方法。var foo = { 答:3, s: 函数() { 警报(一); // a 在此范围内不存在 警报(这个。a); // 'this' 并不总是 foo 警报(foo.a); // 我必须输入“foo”。每个变量之前 // 参考,但我确信这不是这样做的方法 } };
实例 - 未定义
a
。再次强调,这个
并不可靠。var foo = function() { 这个.a = 3; this.s = 函数() { 警报(一); }; }; var foo_instance = new foo(); foo_instance.a = 4; foo_instance.s(); // 错误:a 未定义
实例关闭 - 不返回任何内容;它保持
未定义
。var foo = (function() { 这个.a = 3; this.s = 函数() { 警报(一); }; })(); // foo === 未定义
使用 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:
Simple object - Is not useful due to scoping issues. Using
this.a
would work, butthis
has a different meaning depending on who called it so it's not reliable. For example, I once assigned a function to aWebSocket
class, but all of a suddenthis
referred to theWebSocket
instance when the function was called by aWebSocket
event. I could usebind(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 } };
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
Closure with instance - Doesn't return anything; it stays
undefined
.var foo = (function() { this.a = 3; this.s = function() { alert(a); }; })(); // foo === undefined
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
3 未定义,因为您没有返回任何内容。不要将属性和方法分配给“this”,而是尝试这样做:
foo 现在将返回一个具有定义的属性和方法的对象。这样做你永远不必想知道“this”实际上指的是什么。
3 is undefined because you are not returning anything. instead of assigning properties and methods to 'this', try this:
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.
在我看来,你并没有真正理解
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.
您的第一个代码片段使用闭包,并且对应于 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)