www.webdeveloper.com
Results 1 to 4 of 4

Thread: what's the diff between these 2 methods setup approach?

  1. #1
    Join Date
    Apr 2005
    Posts
    634

    what's the diff between these 2 methods setup approach?

    Hi, what's the diff between the follow way of setting up a method for an object?

    Code:
    function Class1 (){
      this.method = class1Method;
    }
    
    function class1Method(param1, param2){
      //code do something...
    }
    second approach....

    Code:
    function Class2(){
    }//end class2
    
    function class2Method(param1, param2){
      //code do something ...
    }
    
    Class2.prototype.method = class2Method;
    thank you
    "Java is great, PHP sucks and .NET can't scale" - Shaun Connoly, JBoss's vice president of product development. April 5, 2006

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    I dont'd really sense which is your object...

    What about JSON?
    Code:
    <script type="text/javascript">
    var myobj={
    dothis:alert('I did!')
    }
    onload=function(){
    myobj.dothis
    }
    -------
    or maybe:
    Code:
    <script type="text/javascript">
    var myobj={
    dothis:goanddo('I did!')
    }
    onload=function(){
    myobj.dothis
    }
    function goanddo(p){
    alert(p)
    }
    </script>
    Last edited by Kor; 08-03-2006 at 06:29 PM.

  3. #3
    Join Date
    Apr 2005
    Posts
    634
    Quote Originally Posted by Kor
    I dont'd really sense which is your object...
    sorry for not being clear, was trying save space by being lazy heh.
    Here's the long lengthy detail version, let me try again:

    I'm going to make a class call "Circle", its' constructor take 1 paramater "radius" and it has a method "Area()" which will return the area of the circle...

    approach 1:
    Code:
    function Circle(radius){
      this.r = radius;
      this.PI = 3.1416;
    }
    
    function Circle_Area(){
      return this.PI * this.r * this.r;
    }
    
    Circle.prototype.Area = Circle_Area;   //connect Circle method to a function

    and we have approach 2:
    Code:
    function Circle(radius){
      this.r = radius;
      this.PI = 3.1416;
      this.Area = Circle_Area;
    }
    
    function Circle_Area(){
      return this.PI * this.r * this.r;
    }
    and now, I'm going to use the Circle class i just created...

    var c = new Circle(5); //create an instance of Circle class
    var area = c.Area();
    alert(area);

    when run this code, doesn't matter which approach, it'll still alert: 78.54, which is the circle Area I'm expecting.

    which brings me back to my question, what's the diff between approach 1, and approach 2... when one use is better then the others?

    Sorry for the long lengthy post
    "Java is great, PHP sucks and .NET can't scale" - Shaun Connoly, JBoss's vice president of product development. April 5, 2006

  4. #4
    Join Date
    Apr 2005
    Posts
    634

    resolved resolved!!!!!

    I finally figure out the diff between those 2 ways of setting up a method for a Class and I'd like to share to anyone who may be interested:


    the second approach uses a technique call "function literals", that means if we have 1000 circle objects, that Circle_Area() function will have to involve 1000 times (This can be very expensive on memory).


    the first approach uses a technique call "prototype". Prototype means that if a class is inherited from another class, then it can borrow all of its methods, so we don't need to redefine them. Any class that we create has a prototype. This prototype is available to all of the objects of the class. Which means if we have 1000 circle objects, that function will only have to involve once (cheaper on memory).
    "Java is great, PHP sucks and .NET can't scale" - Shaun Connoly, JBoss's vice president of product development. April 5, 2006

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles