private attributes in objects

View: New views
4 Messages — Rating Filter:   Alert me  

private attributes in objects

by memolus@googlemail.com :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Currently I do use such kind of prototypes:

person = new function(name) {
  this.name = name;
}
person.prototype = new function() {
  /* Private */
  var id;

  /* Public */
  this.name = "";
  this.age = 0;
  this.location = "US";
}

The problem is I won't be able to use getter and setter when the new
Ecmascript arrives. But if I switch to object definitions for
prototype I won't be able to use private attributes anymore.
I propose to allow

person.prototype = {
  /* private */
  var id,

  /* public */
  name: "",
  age: 0,
 location: "US"
}
_______________________________________________
es-discuss mailing list
es-discuss@...
https://mail.mozilla.org/listinfo/es-discuss

Re: private attributes in objects

by Brendan Eich-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Oct 11, 2009, at 12:23 PM, memolus@... wrote:

> Currently I do use such kind of prototypes:
>
> person = new function(name) {
>  this.name = name;
> }
> person.prototype = new function() {
>  /* Private */
>  var id;
>
>  /* Public */
>  this.name = "";
>  this.age = 0;
>  this.location = "US";
> }

This sketch could use a function-valued property of this to use the  
private id member, for motivation.


> The problem is I won't be able to use getter and setter when the new
> Ecmascript arrives.

Sorry, I don't know what you mean. What in ES5 prevents you from using  
which getter or setter syntax?

/be

> But if I switch to object definitions for
> prototype I won't be able to use private attributes anymore.
> I propose to allow
>
> person.prototype = {
>  /* private */
>  var id,
>
>  /* public */
>  name: "",
>  age: 0,
> location: "US"
> }
> _______________________________________________
> es-discuss mailing list
> es-discuss@...
> https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
es-discuss@...
https://mail.mozilla.org/listinfo/es-discuss

Re: private attributes in objects

by memolus@googlemail.com :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for your answer.
> This sketch could use a function-valued property of this to use the private id member, for motivation.
Does your expression "a function-valued property of this" mean "method"?

> Sorry, I don't know what you mean. What in ES5 prevents you from using which getter or setter syntax?
You're right, there's the complex "defineProperty" methode I could
use, but they are not so handy.
Anyway why shouldn't I be able to set private attributes in {}-styled
object definitions?
_______________________________________________
es-discuss mailing list
es-discuss@...
https://mail.mozilla.org/listinfo/es-discuss

Re: private attributes in objects

by Brendan Eich-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Oct 11, 2009, at 1:28 PM, memolus@... wrote:

> Thanks for your answer.
>> This sketch could use a function-valued property of this to use the  
>> private id member, for motivation.
> Does your expression "a function-valued property of this" mean  
> "method"?

Sure, informally that's a "method".


>> Sorry, I don't know what you mean. What in ES5 prevents you from  
>> using which getter or setter syntax?
> You're right, there's the complex "defineProperty" methode I could
> use, but they are not so handy.

ES5 standardizes the obj = {get foo() { return 42; }, ...} syntax  
already supported by most browsers out there.


> Anyway why shouldn't I be able to set private attributes in {}-styled
> object definitions?

I'm just trying to understand your post. I didn't say anything against  
the idea of var in object initialisers, was is proposed for Harmony:

http://wiki.ecmascript.org/doku.php?id=strawman:object_initialiser_extensions
http://wiki.ecmascript.org/doku.php?id=strawman:obj_initialiser_private

/be
_______________________________________________
es-discuss mailing list
es-discuss@...
https://mail.mozilla.org/listinfo/es-discuss