How can I set an object's property read-only to public?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andreas M.

    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.
  • Gordon

    #2
    Re: How can I set an object's property read-only to public?

    On May 9, 9:02 am, "Andreas M." <foo...@invalid .invalidwrote:
    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.getPriva te ());

    Comment

    • Jorge

      #3
      Re: How can I set an object's property read-only to public?

      On May 9, 10:02 am, "Andreas M." <foo...@invalid .invalidwrote:
      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.

      Comment

      Working...