JavaScript public field acceptability

I'm just wondering if publicly acceptable fields are acceptable in JavaScript (i.e. those that are not covered by constructor closure). While the usual mantra "does not use public fields, uses accessors or properties", I noticed that properties are not yet widely supported in all browsers (IE).

Other languages, like veins in JavaScript "everything is open", for example, Python, do not seem to care too much about hiding information and public fields, even those that are not decorated with properties. So, can this be done 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;
};
+3
source share
2 answers

, .

( ), . Javascript private, 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. , , , .

+4

JavaScript - JavaScript, . , Java, , Java. . JavaScript Java, . JavaScript - -, , Java , , . , -, JavaScript, .

+1

All Articles