(显示)模块模式,公共变量和返回语句 [英] (Revealing) Module Pattern, public variables and return-statement
问题描述
我试图了解(公开)模块模式中的 public`属性. Carl Danley"揭示模块模式" 是:
I'm trying to understand how public` properties in the (Revealing) Module Pattern work. An advantage pointed out by Carl Danley "The Revealing Module Pattern" is:
明确定义的公共方法和变量,从而提高了可读性
Explicitly defined public methods and variables which lead to increased readability
让我们看一下这段代码(小提琴):
Let's take a look at this code (fiddle):
var a = function() {
var _private = null;
var _public = null;
function init() {
_private = 'private';
_public = 'public';
}
function getPrivate() {
return _private;
}
return {
_public : _public,
init : init,
getPrivate : getPrivate,
}
}();
a.init();
console.log( a._public ); // null
console.log( a.getPrivate() ); // "private"
在调用a._public
时返回null
.我现在可以操纵该公共属性,例如a._public = 'public';
.但是我不能从对象内部更改它.或至少这些更改没有通过.我有点期待它是"public"
,因为它是以前通过init
方法更新的.
It returns null
when calling a._public
. I now can manipulate that public property, like a._public = 'public';
. But I can't change it from within my object. Or at least those changes aren't passed through. I was kinda expecting it to be "public"
as it was updated by the init
-method before.
这实际上是否意味着我没有任何可以处理 public 属性的方法?然后,这种模式下的 public 属性毫无意义,对吧?我也尝试过运气不佳(小提琴):
Does this actually mean, that I can't have any methods, that handle public properties? Then public properties in this pattern make little sense, right? I also tried this without luck (fiddle):
return {
_pubic : _public,
init2 : function() {
_public = 'public';
}
}
最后但并非最不重要的一点是,我对整个return
陈述有疑问.为什么不能仅使用return this;
公开所有内容?因为this
应该是自调用函数的上下文,所以它不应该只是在其中返回eveyrthing吗?为什么我必须创建另一个返回的对象?在此小提琴中,它返回window
对象.
Last, but not least, I have a question regarding the whole return
statement. Why isn't it possible to just use return this;
to make everything public? As this
should be the context of the self-invoked function, shouldn't it just return eveyrthing in it? Why do I have to create another object, that is returned? In this fiddle it returns the window
object.
推荐答案
这实际上是否意味着我没有任何处理公共属性的方法?
Does this actually mean, that I can't have any methods, that handle public properties?
否,这意味着您不能拥有公共的变量. var _public
是一个变量,不能从外部访问,并且在修改私有变量时,这不会反映在您的公共._public
属性中.
No, it means that you cannot have public variables. var _public
is a variable, and it is not accessible from outside, and when you modify the private variable this will not be reflected in your public ._public
property.
如果您想公开发布内容,请使用属性:
If you want to make things public, use properties:
var a = function() {
var _private = null;
function init() {
_private = 'private';
this._public = 'public';
}
function getPrivate() {
return _private;
}
return {
_public : null,
init : init,
getPrivate : getPrivate,
}
}();
我可以操纵该公共财产,例如a._public ='public';.但是我无法在对象内部进行更改.
I can manipulate that public property, like a._public = 'public';. But I can't change it from within my object.
您可以在对象的方法中使用this
,如上所示.或者,您可以使用a
引用该对象,或者甚至可以存储对您返回的对象的本地引用.有关差异,请参见此处.
You can use this
in the methods of your object, as shown above. Or you use a
to reference the object, or possibly even store a local reference to the object you return. See here for the differences.
或者至少这些更改没有通过
Or at least those changes aren't passed through
是的,因为变量与属性不同(与Java等其他语言不同,全局语言除外).在对象文字中导出public: _public
时,它仅从_public
变量中获取当前值,并在该对象上创建一个属性.没有对变量的持久引用,对一个变量的更改也未反映在另一个变量中.
Yes, because variables are different from properties (unlike in some other languages like Java, and with exceptions for global ones). When you export public: _public
in your object literal, it takes only the current value from the _public
variable and creates a property on the object with it. There is no persistent reference to the variable, and changes to one are not reflected in the other.
为什么不能仅使用
return this;
公开所有内容?因为这应该是自调用函数的上下文,所以它不应该只是在其中返回eveyrthing吗?
Why isn't it possible to just use
return this;
to make everything public? As this should be the context of the self-invoked function, shouldn't it just return eveyrthing in it?
变量是JavaScript中作用域的一部分. (除了全局范围之外)这些范围不是该语言可访问的对象.
Variables are part of a scope in JavaScript. (Except for the global one) those scopes are not objects accessible to the language.
this
关键字不涉及该功能的范围,而是涉及调用提供的上下文.可以是方法调用中的基本引用,也可以是构造函数调用中的新实例,或者像您的基本函数调用(或处于松散模式下的全局window
对象)中的任何内容一样.
The this
keyword does not refer to this scope of the function, but to the context that was provided by the call. That can be the base reference in a method call, the new instance in a constructor invocation, or just nothing in a basic function call like yours (or the global window
object in loose mode).
这篇关于(显示)模块模式,公共变量和返回语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!