JavaScript 中公共字段的可接受性

发布于 2024-11-06 00:19:51 字数 514 浏览 0 评论 0原文

我只是想知道 JavaScript 中是否可以接受公共字段(即那些不在构造函数闭包范围内的字段)。虽然通常的口头禅是“不要使用公共字段,使用访问器或属性”,但我注意到属性尚未在所有浏览器 (IE) 上得到广泛支持。

其他与 JavaScript 的“一切都是公共的”类似的语言,比如 Python,似乎并不太关心信息隐藏和公共字段,即使是那些没有用属性修饰的语言。那么,在 JavaScript 中这样做可以吗?

示例

“私人”

var Class = function()
{
    var field = null;
    this.getField = function() { return field; };
    this.setField = function(value) { field = value; };
};

:公共:

var Class = function()
{
    this.field = null;
};

I'm just wondering if public fields (i.e. those not scoped inside the constructor's closure) are acceptable in JavaScript. While the usual mantra is "don't use public fields, use accessors or properties", I noticed that properties are not yet supported widely across all browsers (IE).

Other languages similar in vein to JavaScript's "everything is public" like Python don't seem to care too much about information hiding and public fields, even those not decorated with properties. So, is it okay to do this in JavaScript?

Example

"Private":

var Class = function()
{
    var field = null;
    this.getField = function() { return field; };
    this.setField = function(value) { field = value; };
};

Public:

var Class = function()
{
    this.field = null;
};

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

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

发布评论

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

评论(2

眼眸里的快感 2024-11-13 00:19:51

我们不关心隐藏公共信息,因为来源是开放的并且由客户即时解释。

属性通常不被使用(它们也很慢),并且 getter 函数也不常见。 Javascript 中没有 private 关键字,因此将每个可公开访问的 prop 包装在 getter/setter 方法对中就显得有些过分了。

只需将属性写入您的对象即可。

这是一种常见的做法,甚至可能是一种约定,在内部属性前添加 _ 来表明它们不打算被调用者更改或检查。

例如:

function Foo() { 
    this._meta = {};  // internal use only
    this.prop2 = {};  // public use ok
}

Foo.prototype.meta = function(key, val) {
    if (val !== undefined) {
        this._meta[key] = val;
    } else {
        return this._meta[key];
    }
};

此代码显示了一个公共方法 meta 和一个内部属性 _meta。该属性可以被访问,但开发人员应该意识到,如果他们修改它,他们可能会破坏状态。

We don't care about hiding public information since the source is open and interpreted on the fly by the client.

Properties are not used (they are also slow) commonly and getter functions are also uncommon. There is no private keyword in Javascript, so wrapping each publicly-accessible prop in a getter/setter method pair would be overkill.

Just write out properties to your objects.

It is a common practice, maybe even a convention, to prefix internal properties with a _ to indicate that they are not intended to be changed or examined by callers.

For example :

function Foo() { 
    this._meta = {};  // internal use only
    this.prop2 = {};  // public use ok
}

Foo.prototype.meta = function(key, val) {
    if (val !== undefined) {
        this._meta[key] = val;
    } else {
        return this._meta[key];
    }
};

This code shows a public method meta and an internal property _meta. The property can be accessed, but developers should realize that if they modify it they can corrupt state.

聊慰 2024-11-13 00:19:51

JavaScript 就是 JavaScript,不是任何其他语言。来自 Java 的人们倾向于认为一切都应该像 Java 一样工作。那是错误的。事实上,JavaScript 与 Java 相距甚远,以至于它们的结构规则很少有相似之处。 JavaScript 是一种面向原型、松散类型的函数式语言,因此它可以在一定程度上模仿 Java,但没有理由这样做。一般来说,如果你没有找到关于 JavaScript 的某个咒语的跨语言、常识性原因,请忽略它。

JavaScript is JavaScript, not any other language. People coming from Java have this tendency to assume everything should work like Java. That's wrong. JavaScript is in fact so far away from Java that few of their structural rules are similar. JavaScript is a prototype-oriented, loosely typed functional language, and as such it can mimic Java to an extent, but there is no reason at all why it should. Generally speaking, if you don't find a cros-language, common-sense reason for a certain mantra regarding JavaScript, ignore it.

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