Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Help with OOP javascript

  1. #1
    Join Date
    May 2007
    Location
    Canada
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Help with OOP javascript

    Hi there,

    I'm trying to create a class and having trouble calling a public / privileged method from the class constructor. Here's the example:

    HTML Code:
    function aClassName(param1, param2)
    {
         //constructor begin
    
         this.var1 = param1 ;
         this.var2 = param2 ;
    
         this.doSomething() ;
    
        //constructor end
    
        //privileged function
       
         this.doSomething = function()
         {
              alert("I'm doing something...") ;
         }
    }
    Now, when I try to create a new object of this class,
    HTML Code:
    var myObject = new aClassName("ab", "cd") ;
    the code gets stuck at
    HTML Code:
    this.doSomething() ;
    which is being called from the constructor.

    But if I comment that line and do the following, then everything goes okay.
    HTML Code:
    var myObject = new aClassName("ab", "cd") ;
    myObject.doSomething() ;
    So, I guess, my question is, how can I call a privileged/public function from the constructor?

    Your help is much appreciated. Thank you so much.


    __________________
    Regards,

    Pman
    http://www.pmansLab.com

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Code:
    function ClassAndConstructor(a, b, c) {
    	//public variables
    	this.prop1 = a;
    	this.prop2 = b;
    	this.prop3 = b*c;
    	this.prop4 = 89;
    	this.prop5 = new Array();
    	this.prop5[0] = "foo";
    	this.prop5[1] = {prop1: "gr347!"}
    
    	//private variable
    	var priv1 = "private!";
    
    	//methods
    	this.method1 = function(a) {
    		return a*this.prop3;
    		}
    	this.method2 = function(a) {
    		this.prop1 = a;
    		}
    	this.method3 = function() {
    		return priv1;
    		}
    	this.method4 = function() {
    		return privateMethod1();
    		}
    
    	//private method
    	function privateMethod1() {
    		return "from privateMethod1";
    		}
    	}
    var coolestObectEver = new ClassAndConstructor("interestingString", 4, 11);
    alert(coolestObectEver.prop3); //alerts 44
    alert(coolestObectEver.method1(2)); //alerts 88
    alert(coolestObectEver.prop3); //alerts 44
    alert(coolestObectEver.prop5[1].prop1); //alerts "gr347!"
    alert(coolestObectEver.method3()); //alerts "private!"
    alert(coolestObectEver.method4()); //alerts "from privateMethod1"
    I just included both public and private variables and methods, but (almost all) scripts don't really need to stoop to the level of making private stuff for objects.

    *EDIT*
    I misread your post. In answer, just move the calling of this.doSomething() after you define this.doSomething.
    Last edited by Trinithis; 05-26-2007 at 01:20 AM.

  3. #3
    Join Date
    May 2007
    Location
    Canada
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi Trinithis,

    Thank so much for your reply. Actually my problem was something else and I don't think your example addressed it at all. what I would like is that, when you write

    var coolestObectEver = new ClassAndConstructor("interestingString", 4, 11);

    method1(a) should be called right away after setting all the properties.

    So, basically, in your class / object, there should be a method that users will be able to call at any time like you did by saying

    coolestObectEver.method1(2)

    and that method1 should also be called automatically by the constructor when you create the object by saying

    var coolestObectEver = new ClassAndConstructor("interestingString", 4, 11);

    Let me know, if my question is not clear.

    Thanks

    ________________

    Regards

    Pman
    http://www.pmansLab.com/

  4. #4
    Join Date
    May 2007
    Location
    Canada
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    *EDIT*
    I misread your post. In answer, just move the calling of this.doSomething() after you define this.doSomething.
    Thanks Trinithis. That solved the problem. Was fighting with this for so long. Thanks so much.

  5. #5
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    So, basically, in your class / object, there should be a method that users will be able to call at any time like you did by saying

    coolestObectEver.method1(2)

    and that method1 should also be called automatically by the constructor when you create the object by saying

    var coolestObectEver = new ClassAndConstructor("interestingString", 4, 11);
    The method does not need to be called by the constructor when the object is created. If you want do do something to that effect (like in your example), then yeah, call the method within the constructor after the method is defined.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I'd disagree with the use of "private" data, though. I take a Pythonic approach to this: if something shouldn't be changed, the programmer using it should know that it shouldn't be changed. It shouldn't be inaccessible, because this limits the flexibility of the code. In ECMAScript there's another incentive to make everything public: if you use a closure to allow access to certain data only via privileged methods, that closure is recreated for each instance of the class instead of only once for the prototype, leading to a dramatic decrease in performance.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    May 2007
    Location
    Canada
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    I'd disagree with the use of "private" data, though. I take a Pythonic approach to this: if something shouldn't be changed, the programmer using it should know that it shouldn't be changed. It shouldn't be inaccessible, because this limits the flexibility of the code. In ECMAScript there's another incentive to make everything public: if you use a closure to allow access to certain data only via privileged methods, that closure is recreated for each instance of the class instead of only once for the prototype, leading to a dramatic decrease in performance.
    So, are you saying that it's better to use public methods instead of privileged method?

  8. #8
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Yeah, that's what he's saying, and I agree too. I just included them in my example because you explicitly mentioned "public" stuff, so I put in private types for more completeness.

  9. #9
    Join Date
    May 2007
    Location
    Canada
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks so much for the reply. I'm still trying to get used to Closures in Javascript. I can probably do the basic closure, but I think I'll need more time & lesson for advanced usage of that. Regarding the performance issue, you might be right.

    Since I'm not that experienced in OOP Javascript, I'm not sure about the difference between a privileged and public in javascript.

    Originally Posted by Twey View Post
    I'd disagree with the use of "private" data, though. I take a Pythonic approach to this: if something shouldn't be changed, the programmer using it should know that it shouldn't be changed. It shouldn't be inaccessible, because this limits the flexibility of the code.
    I don't agree with that though. I don't think we should make everything public and assume that the user will be using things appropriately. I guess, it's debatable.

  10. #10
    Join Date
    May 2007
    Location
    Canada
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I need another help from you guys. Now I'm having trouble using setTimeout function inside the class. From my original example, this is what I did

    HTML Code:
    function aClassName(param1, param2)
    {
        //privileged function
       
         this.doSomething = function()
         {
              alert("I'm doing something...") ;
         }
    
         this.run = function()
         {
              setTimeout("this.doSomething()", 500) ;
         }
    
         //constructor begin
    
         this.var1 = param1 ;
         this.var2 = param2 ;
    
         this.doSomething() ;
    
        //constructor end
    
    }
    So, after creating a new object of this class if I execute the run method like this:
    HTML Code:
    var myObj = new aClassName(1, 2) ;
    myObj.run() ;
    it doesn't work. After a lot of googling, it looks like "this" inside setTimeout doesn't refer to the actual object. It refers to the Window. From some of the suggestions, I tried the following:

    HTML Code:
    function aClassName(param1, param2)
    {
        //privileged function
       
         this.doSomething = function()
         {
              alert("I'm doing something...") ;
         }
    
         this.run = function()
         {
              setTimeout("self.doSomething()", 500) ;
         }
    
         //constructor begin
    
         this.var1 = param1 ;
         this.var2 = param2 ;
    
         var self = this ;
    
         this.doSomething() ;
    
        //constructor end
    
    }
    But that didn't work either. Then I used closure and changed the "run" method like this:

    HTML Code:
         this.run = function()
         {
              setTimeout(function() {this.doSomething() ; }, 500) ;
         }
    Is there any other solution to this problem? Thanks again.

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
  •