read only names
This can more or less achieved with getters/setters:
function Obj() {
var x = 'foo';
Object.defineProperty(this, 'x', {
get: function() {
return x;
},
set: function() {
throw new Error('Cannot set property "x"!');
}
});
}
var obj = new Obj();
console.log(obj.x); // 'foo'
obj.x = 'bar'; // Error!
I think ES6 classes will provide some nicer facilities, but I'm not current
on how a public getter / private setter pattern would be achieved outside
of Object.defineProperty()
at this point.
This can more or less achieved with getters/setters: ```js function Obj() { var x = 'foo'; Object.defineProperty(this, 'x', { get: function() { return x; }, set: function() { throw new Error('Cannot set property "x"!'); } }); } var obj = new Obj(); console.log(obj.x); // 'foo' obj.x = 'bar'; // Error! ``` I think ES6 classes will provide some nicer facilities, but I'm not current on how a public getter / private setter pattern would be achieved outside of `Object.defineProperty()` at this point. On Fri, Dec 13, 2013 at 8:34 AM, raul mihaila <raul.mihaila at gmail.com>wrote: > Hello, was there any proposal for object properties that can be read from > anywhere but are writable only from the execution context where the name > associated with the property was defined? Something like: > > function Obj() { > ro x; // read only name x > this.x = 2; > this.setX = function (val) { > this.x = val; > }; > } > > var o = new Obj; > > o.x; // 2 > o.x = 43; // throws some error > o.x; // 2 > o.setX(100); > o.x; // 100 > > The advantages would be that you are sure that there are no side effects > when the property is accessed and also I'm guessing you have no function > call overhead. > > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > > -- Jeremy Martin 661.312.3853 http://devsmash.com @jmar777 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131213/f4101f66/attachment.html>
If you are not paranoid, you can just use nonwritable properties. That forces you to use Object.defineProperty
to change the value, so you are protected against accidental assignments.
function Obj() {
Object.defineProperty(this, 'x', { value: 2, writable: false, enumerable: true, configurable: true })
this.setX = function (val) {
Object.defineProperty(this, 'x', { value: val })
}
}
o = new Obj
o.x = 17 // assignment will fail; moreover, in strict mode, an error is thrown
o.x // 2
o.setX(18)
o.x // 18
It doesn't prevent the user from hanging themself with Object.defineProperty(o, 'x', { value: 17 })
; but at least, it won't be by chance!
If you are not paranoid, you can just use nonwritable properties. That forces you to use `Object.defineProperty` to change the value, so you are protected against accidental assignments. function Obj() { Object.defineProperty(this, 'x', { value: 2, writable: false, enumerable: true, configurable: true }) this.setX = function (val) { Object.defineProperty(this, 'x', { value: val }) } } o = new Obj o.x = 17 // assignment will fail; moreover, in strict mode, an error is thrown o.x // 2 o.setX(18) o.x // 18 It doesn't prevent the user from hanging themself with `Object.defineProperty(o, 'x', { value: 17 })`; but at least, it won't be by chance! —Claude Le 13 déc. 2013 à 14:34, raul mihaila <raul.mihaila at gmail.com> a écrit : > Hello, was there any proposal for object properties that can be read from anywhere but are writable only from the execution context where the name associated with the property was defined? Something like: > > function Obj() { > ro x; // read only name x > this.x = 2; > this.setX = function (val) { > this.x = val; > }; > } > > var o = new Obj; > > o.x; // 2 > o.x = 43; // throws some error > o.x; // 2 > o.setX(100); > o.x; // 100 > > The advantages would be that you are sure that there are no side effects when the property is accessed and also I'm guessing you have no function call overhead. > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131213/736dd1da/attachment.html>
@Jeremy: something similar could be achieved with a private name (I want object properties) and accessor properties, but it lacks the advantages that I mentioned (also outside classes and object literals, the accessor properties are ugly). @Claude: that's not easy to use, and also it can be done outside the execution context.
@Jeremy: something similar could be achieved with a private name (I want object properties) and accessor properties, but it lacks the advantages that I mentioned (also outside classes and object literals, the accessor properties are ugly). @Claude: that's not easy to use, and also it can be done outside the execution context. On Fri, Dec 13, 2013 at 6:20 PM, Claude Pache <claude.pache at gmail.com>wrote: > > If you are not paranoid, you can just use nonwritable properties. That > forces you to use `Object.defineProperty` to change the value, so you are > protected against accidental assignments. > > function Obj() { > Object.defineProperty(this, 'x', { value: 2, writable: false, > enumerable: true, configurable: true }) > this.setX = function (val) { > Object.defineProperty(this, 'x', { value: val }) > } > } > > o = new Obj > o.x = 17 // assignment will fail; moreover, in strict mode, an error > is thrown > o.x // 2 > o.setX(18) > o.x // 18 > > It doesn't prevent the user from hanging themself with > `Object.defineProperty(o, 'x', { value: 17 })`; but at least, it won't be > by chance! > > —Claude > > > Le 13 déc. 2013 à 14:34, raul mihaila <raul.mihaila at gmail.com> a écrit : > > Hello, was there any proposal for object properties that can be read from > anywhere but are writable only from the execution context where the name > associated with the property was defined? Something like: > > function Obj() { > ro x; // read only name x > this.x = 2; > this.setX = function (val) { > this.x = val; > }; > } > > var o = new Obj; > > o.x; // 2 > o.x = 43; // throws some error > o.x; // 2 > o.setX(100); > o.x; // 100 > > The advantages would be that you are sure that there are no side effects > when the property is accessed and also I'm guessing you have no function > call overhead. > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131213/82f9c561/attachment-0001.html>
the closer/best option would be probably this one:
function Obj() {
var xValue = 2;
Object.defineProperties(this, {
x: {
enumerable: true,
configurable: false,
get: function x() {
return xValue;
},
set: function x(value) {
throw 'not allowed';
}
},
setX: {
enumerable: true,
configurable: false,
writable: false,
value: function setX(value) {
xValue = value;
}
}
});
}
However, this patter stinks for many reasons:
- bad performance per each object initialization, avoid if you need to create many Obj instances
- it's unclear why the
setX()
is better than just using the setter ... you have a public directly accessible property but you want a method to set its value ... you are not making its value settable only privatly, you are exposing the possibility so you gain nothing in terms of "security"
Best
the closer/best option would be probably this one: ```javascript function Obj() { var xValue = 2; Object.defineProperties(this, { x: { enumerable: true, configurable: false, get: function x() { return xValue; }, set: function x(value) { throw 'not allowed'; } }, setX: { enumerable: true, configurable: false, writable: false, value: function setX(value) { xValue = value; } } }); } ``` However, this patter stinks for many reasons: 1. bad performance per each object initialization, avoid if you need to create many Obj instances 2. it's unclear why the `setX()` is better than just using the setter ... you have a public directly accessible property but you want a method to set its value ... you are not making its value settable only privatly, you are exposing the possibility so you gain nothing in terms of "security" Best Regards On Fri, Dec 13, 2013 at 8:48 AM, raul mihaila <raul.mihaila at gmail.com>wrote: > @Jeremy: something similar could be achieved with a private name (I want > object properties) and accessor properties, but it lacks the advantages > that I mentioned (also outside classes and object literals, the accessor > properties are ugly). > @Claude: that's not easy to use, and also it can be done outside the > execution context. > > > On Fri, Dec 13, 2013 at 6:20 PM, Claude Pache <claude.pache at gmail.com>wrote: > >> >> If you are not paranoid, you can just use nonwritable properties. That >> forces you to use `Object.defineProperty` to change the value, so you are >> protected against accidental assignments. >> >> function Obj() { >> Object.defineProperty(this, 'x', { value: 2, writable: false, >> enumerable: true, configurable: true }) >> this.setX = function (val) { >> Object.defineProperty(this, 'x', { value: val }) >> } >> } >> >> o = new Obj >> o.x = 17 // assignment will fail; moreover, in strict mode, an error >> is thrown >> o.x // 2 >> o.setX(18) >> o.x // 18 >> >> It doesn't prevent the user from hanging themself with >> `Object.defineProperty(o, 'x', { value: 17 })`; but at least, it won't be >> by chance! >> >> —Claude >> >> >> Le 13 déc. 2013 à 14:34, raul mihaila <raul.mihaila at gmail.com> a écrit : >> >> Hello, was there any proposal for object properties that can be read from >> anywhere but are writable only from the execution context where the name >> associated with the property was defined? Something like: >> >> function Obj() { >> ro x; // read only name x >> this.x = 2; >> this.setX = function (val) { >> this.x = val; >> }; >> } >> >> var o = new Obj; >> >> o.x; // 2 >> o.x = 43; // throws some error >> o.x; // 2 >> o.setX(100); >> o.x; // 100 >> >> The advantages would be that you are sure that there are no side effects >> when the property is accessed and also I'm guessing you have no function >> call overhead. >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> >> >> > > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131213/1fdef1e2/attachment.html>
P.S. I swear I thought this was JSMentors ML ... sorry for that
P.S. I swear I thought this was JSMentors ML ... sorry for that On Fri, Dec 13, 2013 at 10:19 AM, Andrea Giammarchi < andrea.giammarchi at gmail.com> wrote: > the closer/best option would be probably this one: > > ```javascript > function Obj() { > var xValue = 2; > Object.defineProperties(this, { > x: { > enumerable: true, > configurable: false, > get: function x() { > return xValue; > }, > set: function x(value) { > throw 'not allowed'; > } > }, > setX: { > enumerable: true, > configurable: false, > writable: false, > value: function setX(value) { > xValue = value; > } > } > }); > } > ``` > > However, this patter stinks for many reasons: > > 1. bad performance per each object initialization, avoid if you need > to create many Obj instances > 2. it's unclear why the `setX()` is better than just using the setter > ... you have a public directly accessible property but you want a method to > set its value ... you are not making its value settable only privatly, you > are exposing the possibility so you gain nothing in terms of "security" > > Best Regards > > > > > > On Fri, Dec 13, 2013 at 8:48 AM, raul mihaila <raul.mihaila at gmail.com>wrote: > >> @Jeremy: something similar could be achieved with a private name (I want >> object properties) and accessor properties, but it lacks the advantages >> that I mentioned (also outside classes and object literals, the accessor >> properties are ugly). >> @Claude: that's not easy to use, and also it can be done outside the >> execution context. >> >> >> On Fri, Dec 13, 2013 at 6:20 PM, Claude Pache <claude.pache at gmail.com>wrote: >> >>> >>> If you are not paranoid, you can just use nonwritable properties. That >>> forces you to use `Object.defineProperty` to change the value, so you are >>> protected against accidental assignments. >>> >>> function Obj() { >>> Object.defineProperty(this, 'x', { value: 2, writable: false, >>> enumerable: true, configurable: true }) >>> this.setX = function (val) { >>> Object.defineProperty(this, 'x', { value: val }) >>> } >>> } >>> >>> o = new Obj >>> o.x = 17 // assignment will fail; moreover, in strict mode, an error >>> is thrown >>> o.x // 2 >>> o.setX(18) >>> o.x // 18 >>> >>> It doesn't prevent the user from hanging themself with >>> `Object.defineProperty(o, 'x', { value: 17 })`; but at least, it won't be >>> by chance! >>> >>> —Claude >>> >>> >>> Le 13 déc. 2013 à 14:34, raul mihaila <raul.mihaila at gmail.com> a écrit : >>> >>> Hello, was there any proposal for object properties that can be read >>> from anywhere but are writable only from the execution context where the >>> name associated with the property was defined? Something like: >>> >>> function Obj() { >>> ro x; // read only name x >>> this.x = 2; >>> this.setX = function (val) { >>> this.x = val; >>> }; >>> } >>> >>> var o = new Obj; >>> >>> o.x; // 2 >>> o.x = 43; // throws some error >>> o.x; // 2 >>> o.setX(100); >>> o.x; // 100 >>> >>> The advantages would be that you are sure that there are no side effects >>> when the property is accessed and also I'm guessing you have no function >>> call overhead. >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >>> >>> >> >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131213/1748f363/attachment-0001.html>
I used setX only to show that the value can be set with a method. The scope for ro names could be the same as for private names. Btw, are private names still in? I couldn't find them in the html version of the latest draft.
I used setX only to show that the value can be set with a method. The scope for ro names could be the same as for private names. Btw, are private names still in? I couldn't find them in the html version of the latest draft. On Fri, Dec 13, 2013 at 8:19 PM, Andrea Giammarchi < andrea.giammarchi at gmail.com> wrote: > the closer/best option would be probably this one: > > ```javascript > function Obj() { > var xValue = 2; > Object.defineProperties(this, { > x: { > enumerable: true, > configurable: false, > get: function x() { > return xValue; > }, > set: function x(value) { > throw 'not allowed'; > } > }, > setX: { > enumerable: true, > configurable: false, > writable: false, > value: function setX(value) { > xValue = value; > } > } > }); > } > ``` > > However, this patter stinks for many reasons: > > 1. bad performance per each object initialization, avoid if you need > to create many Obj instances > 2. it's unclear why the `setX()` is better than just using the setter > ... you have a public directly accessible property but you want a method to > set its value ... you are not making its value settable only privatly, you > are exposing the possibility so you gain nothing in terms of "security" > > Best Regards > > > > > > On Fri, Dec 13, 2013 at 8:48 AM, raul mihaila <raul.mihaila at gmail.com>wrote: > >> @Jeremy: something similar could be achieved with a private name (I want >> object properties) and accessor properties, but it lacks the advantages >> that I mentioned (also outside classes and object literals, the accessor >> properties are ugly). >> @Claude: that's not easy to use, and also it can be done outside the >> execution context. >> >> >> On Fri, Dec 13, 2013 at 6:20 PM, Claude Pache <claude.pache at gmail.com>wrote: >> >>> >>> If you are not paranoid, you can just use nonwritable properties. That >>> forces you to use `Object.defineProperty` to change the value, so you are >>> protected against accidental assignments. >>> >>> function Obj() { >>> Object.defineProperty(this, 'x', { value: 2, writable: false, >>> enumerable: true, configurable: true }) >>> this.setX = function (val) { >>> Object.defineProperty(this, 'x', { value: val }) >>> } >>> } >>> >>> o = new Obj >>> o.x = 17 // assignment will fail; moreover, in strict mode, an error >>> is thrown >>> o.x // 2 >>> o.setX(18) >>> o.x // 18 >>> >>> It doesn't prevent the user from hanging themself with >>> `Object.defineProperty(o, 'x', { value: 17 })`; but at least, it won't be >>> by chance! >>> >>> —Claude >>> >>> >>> Le 13 déc. 2013 à 14:34, raul mihaila <raul.mihaila at gmail.com> a écrit : >>> >>> Hello, was there any proposal for object properties that can be read >>> from anywhere but are writable only from the execution context where the >>> name associated with the property was defined? Something like: >>> >>> function Obj() { >>> ro x; // read only name x >>> this.x = 2; >>> this.setX = function (val) { >>> this.x = val; >>> }; >>> } >>> >>> var o = new Obj; >>> >>> o.x; // 2 >>> o.x = 43; // throws some error >>> o.x; // 2 >>> o.setX(100); >>> o.x; // 100 >>> >>> The advantages would be that you are sure that there are no side effects >>> when the property is accessed and also I'm guessing you have no function >>> call overhead. >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >>> >>> >> >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131213/4bb4fed1/attachment.html>
Correction: The scope for ro names could be the same as for private names, except that they can be read from outside.
Correction: The scope for ro names could be the same as for private names, except that they can be read from outside. On Fri, Dec 13, 2013 at 8:39 PM, Andrea Giammarchi < andrea.giammarchi at gmail.com> wrote: > P.S. I swear I thought this was JSMentors ML ... sorry for that > > > On Fri, Dec 13, 2013 at 10:19 AM, Andrea Giammarchi < > andrea.giammarchi at gmail.com> wrote: > >> the closer/best option would be probably this one: >> >> ```javascript >> function Obj() { >> var xValue = 2; >> Object.defineProperties(this, { >> x: { >> enumerable: true, >> configurable: false, >> get: function x() { >> return xValue; >> }, >> set: function x(value) { >> throw 'not allowed'; >> } >> }, >> setX: { >> enumerable: true, >> configurable: false, >> writable: false, >> value: function setX(value) { >> xValue = value; >> } >> } >> }); >> } >> ``` >> >> However, this patter stinks for many reasons: >> >> 1. bad performance per each object initialization, avoid if you need >> to create many Obj instances >> 2. it's unclear why the `setX()` is better than just using the setter >> ... you have a public directly accessible property but you want a method to >> set its value ... you are not making its value settable only privatly, you >> are exposing the possibility so you gain nothing in terms of "security" >> >> Best Regards >> >> >> >> >> >> On Fri, Dec 13, 2013 at 8:48 AM, raul mihaila <raul.mihaila at gmail.com>wrote: >> >>> @Jeremy: something similar could be achieved with a private name (I want >>> object properties) and accessor properties, but it lacks the advantages >>> that I mentioned (also outside classes and object literals, the accessor >>> properties are ugly). >>> @Claude: that's not easy to use, and also it can be done outside the >>> execution context. >>> >>> >>> On Fri, Dec 13, 2013 at 6:20 PM, Claude Pache <claude.pache at gmail.com>wrote: >>> >>>> >>>> If you are not paranoid, you can just use nonwritable properties. That >>>> forces you to use `Object.defineProperty` to change the value, so you are >>>> protected against accidental assignments. >>>> >>>> function Obj() { >>>> Object.defineProperty(this, 'x', { value: 2, writable: false, >>>> enumerable: true, configurable: true }) >>>> this.setX = function (val) { >>>> Object.defineProperty(this, 'x', { value: val }) >>>> } >>>> } >>>> >>>> o = new Obj >>>> o.x = 17 // assignment will fail; moreover, in strict mode, an >>>> error is thrown >>>> o.x // 2 >>>> o.setX(18) >>>> o.x // 18 >>>> >>>> It doesn't prevent the user from hanging themself with >>>> `Object.defineProperty(o, 'x', { value: 17 })`; but at least, it won't be >>>> by chance! >>>> >>>> —Claude >>>> >>>> >>>> Le 13 déc. 2013 à 14:34, raul mihaila <raul.mihaila at gmail.com> a écrit >>>> : >>>> >>>> Hello, was there any proposal for object properties that can be read >>>> from anywhere but are writable only from the execution context where the >>>> name associated with the property was defined? Something like: >>>> >>>> function Obj() { >>>> ro x; // read only name x >>>> this.x = 2; >>>> this.setX = function (val) { >>>> this.x = val; >>>> }; >>>> } >>>> >>>> var o = new Obj; >>>> >>>> o.x; // 2 >>>> o.x = 43; // throws some error >>>> o.x; // 2 >>>> o.setX(100); >>>> o.x; // 100 >>>> >>>> The advantages would be that you are sure that there are no side >>>> effects when the property is accessed and also I'm guessing you have no >>>> function call overhead. >>>> _______________________________________________ >>>> es-discuss mailing list >>>> es-discuss at mozilla.org >>>> https://mail.mozilla.org/listinfo/es-discuss >>>> >>>> >>>> >>> >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >>> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131213/889f243b/attachment.html>
then with current status you need to access the caller
against known
methods ... as example:
var Obj = (function(){
var
ObjProto = Obj.prototype,
methods
;
function Obj() {
this.x = 2;
}
Object.defineProperties(ObjProto, {
x: {
enumerable: true,
configurable: false,
get: function x() {
return this._x;
},
set: function x(value) {
if (methods.indexOf(x.caller) < 0) throw 'nope';
this._x = value;
}
}
});
methods = [Obj].concat(
Object.getOwnPropertyNames(ObjProto)
.filter(function(p){
return typeof ObjProto[p] === 'function';
})
.map(function(p){
return ObjProto[p];
})
);
return Obj;
}());
var o = new Obj;
o.x; // 2
try {
o.x = 3;
} catch(o_O) {
o_O == 'nope'; // true
}
watch out caller
access will throw if you are putting that code under
'use strict' directive.
Just hinting solutions, nothing about specs that I recognize ... Symbols are private though but not sure the latest state regarding these.
Best
then with current status you need to access the `caller` against known methods ... as example: ```javascript var Obj = (function(){ var ObjProto = Obj.prototype, methods ; function Obj() { this.x = 2; } Object.defineProperties(ObjProto, { x: { enumerable: true, configurable: false, get: function x() { return this._x; }, set: function x(value) { if (methods.indexOf(x.caller) < 0) throw 'nope'; this._x = value; } } }); methods = [Obj].concat( Object.getOwnPropertyNames(ObjProto) .filter(function(p){ return typeof ObjProto[p] === 'function'; }) .map(function(p){ return ObjProto[p]; }) ); return Obj; }()); var o = new Obj; o.x; // 2 try { o.x = 3; } catch(o_O) { o_O == 'nope'; // true } ``` watch out `caller` access will throw if you are putting that code under 'use strict' directive. Just hinting solutions, nothing about specs that I recognize ... Symbols are private though but not sure the latest state regarding these. Best Regards On Fri, Dec 13, 2013 at 10:46 AM, raul mihaila <raul.mihaila at gmail.com>wrote: > Correction: The scope for ro names could be the same as for private names, > except that they can be read from outside. > > > On Fri, Dec 13, 2013 at 8:39 PM, Andrea Giammarchi < > andrea.giammarchi at gmail.com> wrote: > >> P.S. I swear I thought this was JSMentors ML ... sorry for that >> >> >> On Fri, Dec 13, 2013 at 10:19 AM, Andrea Giammarchi < >> andrea.giammarchi at gmail.com> wrote: >> >>> the closer/best option would be probably this one: >>> >>> ```javascript >>> function Obj() { >>> var xValue = 2; >>> Object.defineProperties(this, { >>> x: { >>> enumerable: true, >>> configurable: false, >>> get: function x() { >>> return xValue; >>> }, >>> set: function x(value) { >>> throw 'not allowed'; >>> } >>> }, >>> setX: { >>> enumerable: true, >>> configurable: false, >>> writable: false, >>> value: function setX(value) { >>> xValue = value; >>> } >>> } >>> }); >>> } >>> ``` >>> >>> However, this patter stinks for many reasons: >>> >>> 1. bad performance per each object initialization, avoid if you need >>> to create many Obj instances >>> 2. it's unclear why the `setX()` is better than just using the >>> setter ... you have a public directly accessible property but you want a >>> method to set its value ... you are not making its value settable only >>> privatly, you are exposing the possibility so you gain nothing in terms of >>> "security" >>> >>> Best Regards >>> >>> >>> >>> >>> >>> On Fri, Dec 13, 2013 at 8:48 AM, raul mihaila <raul.mihaila at gmail.com>wrote: >>> >>>> @Jeremy: something similar could be achieved with a private name (I >>>> want object properties) and accessor properties, but it lacks the >>>> advantages that I mentioned (also outside classes and object literals, the >>>> accessor properties are ugly). >>>> @Claude: that's not easy to use, and also it can be done outside the >>>> execution context. >>>> >>>> >>>> On Fri, Dec 13, 2013 at 6:20 PM, Claude Pache <claude.pache at gmail.com>wrote: >>>> >>>>> >>>>> If you are not paranoid, you can just use nonwritable properties. That >>>>> forces you to use `Object.defineProperty` to change the value, so you are >>>>> protected against accidental assignments. >>>>> >>>>> function Obj() { >>>>> Object.defineProperty(this, 'x', { value: 2, writable: false, >>>>> enumerable: true, configurable: true }) >>>>> this.setX = function (val) { >>>>> Object.defineProperty(this, 'x', { value: val }) >>>>> } >>>>> } >>>>> >>>>> o = new Obj >>>>> o.x = 17 // assignment will fail; moreover, in strict mode, an >>>>> error is thrown >>>>> o.x // 2 >>>>> o.setX(18) >>>>> o.x // 18 >>>>> >>>>> It doesn't prevent the user from hanging themself with >>>>> `Object.defineProperty(o, 'x', { value: 17 })`; but at least, it won't be >>>>> by chance! >>>>> >>>>> —Claude >>>>> >>>>> >>>>> Le 13 déc. 2013 à 14:34, raul mihaila <raul.mihaila at gmail.com> a >>>>> écrit : >>>>> >>>>> Hello, was there any proposal for object properties that can be read >>>>> from anywhere but are writable only from the execution context where the >>>>> name associated with the property was defined? Something like: >>>>> >>>>> function Obj() { >>>>> ro x; // read only name x >>>>> this.x = 2; >>>>> this.setX = function (val) { >>>>> this.x = val; >>>>> }; >>>>> } >>>>> >>>>> var o = new Obj; >>>>> >>>>> o.x; // 2 >>>>> o.x = 43; // throws some error >>>>> o.x; // 2 >>>>> o.setX(100); >>>>> o.x; // 100 >>>>> >>>>> The advantages would be that you are sure that there are no side >>>>> effects when the property is accessed and also I'm guessing you have no >>>>> function call overhead. >>>>> _______________________________________________ >>>>> es-discuss mailing list >>>>> es-discuss at mozilla.org >>>>> https://mail.mozilla.org/listinfo/es-discuss >>>>> >>>>> >>>>> >>>> >>>> _______________________________________________ >>>> es-discuss mailing list >>>> es-discuss at mozilla.org >>>> https://mail.mozilla.org/listinfo/es-discuss >>>> >>>> >>> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131213/ab594c0d/attachment-0001.html>
raul mihaila wrote:
Btw, are private names still in? I couldn't find them in the html version of the latest draft.
You can tell that private names are not in by noticing the strawman: namespace in the wiki, not the harmony: namespace.
I don't think it's wise to build on them yet. The objections are recorded in es-discuss and meeting notes:
esdiscuss.org/topic/private-names-in-text-javascript
etc.
raul mihaila wrote: > Btw, are private names still in? I couldn't find them in the html > version of the latest draft. You can tell that private names are *not* in by noticing the strawman: namespace in the wiki, not the harmony: namespace. http://wiki.ecmascript.org/doku.php?id=strawman:private_names I don't think it's wise to build on them yet. The objections are recorded in es-discuss and meeting notes: http://esdiscuss.org/topic/private-names-in-text-javascript etc. /be
thanks
thanks On Sat, Dec 14, 2013 at 12:46 AM, Brendan Eich <brendan at mozilla.com> wrote: > raul mihaila wrote: > >> Btw, are private names still in? I couldn't find them in the html version >> of the latest draft. >> > > You can tell that private names are *not* in by noticing the strawman: > namespace in the wiki, not the harmony: namespace. > > http://wiki.ecmascript.org/doku.php?id=strawman:private_names > > I don't think it's wise to build on them yet. The objections are recorded > in es-discuss and meeting notes: > > http://esdiscuss.org/topic/private-names-in-text-javascript > > etc. > > /be > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131214/419e4d9e/attachment.html>
Was there any proposal for object properties that can be read from anywhere but are writable only from the execution context where the name associated with the property was defined? Something like:
function Obj() { ro x; // read only name x this.x = 2; this.setX = function (val) { this.x = val; }; } var o = new Obj; o.x; // 2 o.x = 43; // throws some error o.x; // 2 o.setX(100); o.x; // 100
The advantages would be that you are sure that there are no side effects when the property is accessed and also I'm guessing you have no function call overhead.