Google Groups Home
Help | Sign in
How can I set an object's property read-only to public?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Andreas M.  
View profile
 More options May 9, 4:02 am
Newsgroups: comp.lang.javascript
From: "Andreas M." <foo...@invalid.invalid>
Date: Fri, 09 May 2008 10:02:27 +0200
Local: Fri, May 9 2008 4:02 am
Subject: How can I set an object's property read-only to public?
I have an object, that contains a status-property. The status gets set
by a function internal to that object. This function gets invoked as
/init();/ and sets the status-property of the outer function.

If I make that property world-readable via /this/ it will become
world-writable, too. Is there any way except of defining a getter
function to an inner property? I would like to get the property via
/Object.property/ still.

--
Bye,
Andreas M.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gordon  
View profile
 More options May 9, 4:40 am
Newsgroups: comp.lang.javascript
From: Gordon <gordon.mc...@ntlworld.com>
Date: Fri, 9 May 2008 01:40:58 -0700 (PDT)
Local: Fri, May 9 2008 4:40 am
Subject: Re: How can I set an object's property read-only to public?
On May 9, 9:02 am, "Andreas M." <foo...@invalid.invalid> wrote:

> I have an object, that contains a status-property. The status gets set
> by a function internal to that object. This function gets invoked as
> /init();/ and sets the status-property of the outer function.

> If I make that property world-readable via /this/ it will become
> world-writable, too. Is there any way except of defining a getter
> function to an inner property? I would like to get the property via
> /Object.property/ still.

> --
> Bye,
> Andreas M.

Make it a private property and use a getter.

For example:

function myClass ()
{
    this.public = 0; // This property is fully accessible from the
outside
    var private = 1; // This property is inaccessible from outside the
object

    this.getPrivate = function ()
    {
        return (private);
    }

}

myObj = new myClass ();

alert (myObj.getPrivate ());


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jorge  
View profile
 More options May 9, 4:48 am
Newsgroups: comp.lang.javascript
From: Jorge <jo...@jorgechamorro.com>
Date: Fri, 9 May 2008 01:48:58 -0700 (PDT)
Local: Fri, May 9 2008 4:48 am
Subject: Re: How can I set an object's property read-only to public?
On May 9, 10:02 am, "Andreas M." <foo...@invalid.invalid> wrote:

> I have an object, that contains a status-property. The status gets set
> by a function internal to that object. This function gets invoked as
> /init();/ and sets the status-property of the outer function.

> If I make that property world-readable via /this/ it will become
> world-writable, too. Is there any way except of defining a getter
> function to an inner property? I would like to get the property via
> /Object.property/ still.

No. There isn't.
Any (non-native) Object.property is writable as long as it's visible.
Reducing the visibility of the status-property (hiding it into a
closure) would protect it, but its getter() would still be r/w anyway,
it could be hijacked to return something != status-property. Viva the
mutability.

--Jorge.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google