SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Guru
    Join Date
    Feb 2005
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript OOP question: accessing private variables

    AFAIK defining functions inside the constructor is inefficient, since it would have to execute the function expression every time the constructor is called. However, in order to access private variables, wouldn't defining the functions inside the constructor be necessary?

    Is there any simple way to avoid this problem? Right now, I'm resorting to some hacks, which make me wonder if it's worth the effort (EDIT: following code doesn't work):

    Code:
    function someClass() {
    	this.constructor.apply(this, arguments);
    }
    someClass.prototype = new function() {
    	//constructor:
    	var self = this;
    	self.constructor = function(a, b) {
    		privateMember = a;
    		self.publicMember = b;
    	}
    	
    	//private:
    	var privateMember;
    	
    	var privateMethod = function() {
    		alert(privateMember);
    		alert(self.publicMember);
    	}
    
    	//public:
    	self.publicMember;	//actually does nothing; just informative
    
    	self.publicMethod = function() {
    		privateMethod();
    	}
    }
    
    //example usage:
    var someObject = new someClass(10, 20);
    someObject.publicMethod();
    alert(someObject.publicMember);
    Last edited by Maian; Apr 20, 2005 at 18:08.

  2. #2
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm just beginning to build a POO framework in Javascript more or less inspired from Java
    do you see problem with this code below ?

    Code:
    <script type="text/javascript">
    <!--
    /***************************************************************
    * Constructor
    ***************************************************************/ 
    function MessageApp() { 
    
    /***************************************************************
    * private member: can only be accessed 
    * by getter and setter methods
    ***************************************************************/ 
       var Message;    
    
    /***************************************************************
    * getter and setter methods: can access private members 
    * can be called by external functions
    ***************************************************************/    
       this.getMessage = function(sender) {          
            return Message;
       }
       
       this.setMessage = function(s_message,sender) {        
            Message = s_message;
            return Message;
       }     
    }
    //--> 
    </script>
    
    <script type="text/javascript">
    <!--  
    /***************************************************************
    * Instantiation
    ***************************************************************/ 
    myApp = new MessageApp();
    //--> 
    </script>
    
    <script type="text/javascript">
    <!--
    /***************************************************************
    * Usage
    ***************************************************************/ 
    myApp.setMessage("Hello World");
    var Message = myApp.getMessage();
    alert(Message);
    //-->
    </script>

  3. #3
    SitePoint Guru
    Join Date
    Feb 2005
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks okay to me, though that sender parameter isn't being used at all.

    BTW, the code in my first post doesn't work because the "private" variables would be static to the class, rather than of the object. Also, I figured that there shouldn't be much of a performance impact of defining functions in the constructor, since a good JS engine would "cache" the defined function (called "joining" in the ECMAScript spec).

  4. #4
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Maian
    Looks okay to me, though that sender parameter isn't being used at all.
    I said MORE or LESS inspired from JAVA because secretly there is also a bit of DELPHI . Delphi passes the sender and I remember some pain in the neck with VB because VB doesn't do that that's why I thought about passing the sender systematically in case I need to use it (for example for check the type of the sender to give it authorisation or not to use the method).

    Quote Originally Posted by Maian
    BTW, the code in my first post doesn't work because the "private" variables would be static to the class, rather than of the object. Also, I figured that there shouldn't be much of a performance impact of defining functions in the constructor, since a good JS engine would "cache" the defined function (called "joining" in the ECMAScript spec).
    I don't know much about javascript, I just try to transpose some fundamental concepts that exist in other languages I'm used to so I should read the spec of javascript I just fear a headache if only there was a summary

  5. #5
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For those who wants to understand the code I have already posted a good site on JS POO I repost it :
    http://www.crockford.com/

  6. #6
    SitePoint Guru
    Join Date
    Feb 2005
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Er, it's OOP not POO

  7. #7
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Maian
    Er, it's OOP not POO
    Euh yes thanks it's because in french we say "Programmation Orienté Objet" so POO

  8. #8
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Maian
    Looks okay to me, though that sender parameter isn't being used at all.

    BTW, the code in my first post doesn't work because the "private" variables would be static to the class, rather than of the object. Also, I figured that there shouldn't be much of a performance impact of defining functions in the constructor, since a good JS engine would "cache" the defined function (called "joining" in the ECMAScript spec).
    haven't read the current Ecma spec but have just quickly seen Netscape proposal for Ecma 4 for next version of Javascript 2 and it should include private, namespace and package like Java .

    That would be better because it's confusing when coming from Java - and any other traditional compilers in fact - to see
    'var something'
    in an object to behave like an instance member that persists from call to call whereas syntaxically it should be a local variable only.

  9. #9
    SitePoint Guru
    Join Date
    Feb 2005
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The status of that Javascript 2 proposal hasn't been updated in over a year or more. I don't know of the project is suspended or dead or what.

  10. #10
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Maian
    The status of that Javascript 2 proposal hasn't been updated in over a year or more. I don't know of the project is suspended or dead or what.
    It's not dead. Flash developers are already happy with Javascript 2.

  11. #11
    SitePoint Guru
    Join Date
    Feb 2005
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well it seems to be dead wrt Ecma.

  12. #12
    SitePoint Addict
    Join Date
    May 2004
    Location
    Europe
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JScript.NET doesn't look that different from the JS2 proposal either, there has been some vague hint of a JS2 implementation in Mozilla some time this summer and the ECMA working group is still alive.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •