JavaScript 模块模式 - 使用“return this”怎么样?
在阅读了一些有关模块模式的内容之后,我看到了几种返回您想要公开的属性的方法。
最常见的方法之一是在“return”语句中声明公共属性和方法,除了私有属性和方法之外。类似的方式(“揭示”模式)是提供对您想要公开的属性和方法的简单引用。最后,我看到的第三种技术是在模块函数内创建一个新对象,在返回该对象之前向该对象分配新属性。这是一个有趣的想法,但需要创建一个新对象。
所以我在想,为什么不直接使用 this.propertyName
来分配你的公共属性和方法,最后使用 return this
呢?这种方式对我来说似乎更简单,因为您可以使用通常的 var
或 function
语法创建私有属性和方法,或者使用 this.propertyName
语法来声明您的公共方法。
这是我建议的方法:
(function() {
var privateMethod = function () {
alert('This is a private method.');
}
this.publicMethod = function () {
alert('This is a public method.');
}
return this;
})();
使用上述方法有什么优点/缺点吗?其他人呢?
After doing some reading about the Module Pattern, I've seen a few ways of returning the properties which you want to be public.
One of the most common ways is to declare your public properties and methods right inside of the "return" statement, apart from your private properties and methods. A similar way (the "Revealing" pattern) is to provide simply references to the properties and methods which you want to be public. Lastly, a third technique I saw was to create a new object inside your module function, to which you assign your new properties before returning said object. This was an interesting idea, but requires the creation of a new object.
So I was thinking, why not just use this.propertyName
to assign your public properties and methods, and finally use return this
at the end? This way seems much simpler to me, as you can create private properties and methods with the usual var
or function
syntax, or use the this.propertyName
syntax to declare your public methods.
Here's the method I'm suggesting:
(function() {
var privateMethod = function () {
alert('This is a private method.');
}
this.publicMethod = function () {
alert('This is a public method.');
}
return this;
})();
Are there any pros/cons to using the method above? What about the others?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的函数没有对象上下文,因此在本例中
this
引用了全局window
对象。您分配给this
的每个属性都会自动污染全局命名空间。您可以显式地向其传递一个对象来告知要使用哪个上下文。
您可以用其他风格编写:
我们已经到达 讨论了模式。有关更多详细信息,请参阅 Dustin 关于确定匿名函数范围的文章。
Your function has no object context, so
this
references to the globalwindow
object in this case. Every property you assign tothis
automatically pollutes the global namespace.You can explicitly pass it an object to tell which context to use.
Which you can write in somewhat other style:
And we arrived to an already discussed pattern. For further details, see Dustin's article on Scoping anonymous functions.
我建议您将公共属性和方法添加到然后返回的匿名对象中:
I would recommend the style where you add your public properties and methods to an anonymous object that you then return:
如果您想发布方法,请执行以下操作:
if you want to publish methods, then do something like:
另一种选择是完全避免 this 引用。定义一个创建并返回匿名对象的函数。
Another option is to avoid the this reference altogether. Define a function that creates and returns an anonymous object instead.
揭示模块模式:
此外,如果您需要方法链接:
还有很多方法,您也可以让您的模块成为主角。
Revealing Module patterns:
Also, if you need method-chaining:
There's also plenty more ways, and you can protagonize your modules as well.