SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: JS Constructor

  1. #1
    SitePoint Enthusiast Grayson's Avatar
    Join Date
    Jun 2008
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JS Constructor

    Could anyone tell me the signifance of this type of insantation. This example has defined 2 methods and assigned 3 properties.


    JSClass.html:

    Code javascript:
    <script type='text/javascript'
     
    function Car( make, model, year )
    {
          var _pp={'_make':make,'_model':model,'_year':year}
          this.get=function(pp){return eval('_pp._'+pp);}
          this.toString=function(){var s='<br/><b>Car</b><br/>';
              for(i in _pp)s+='<i>'+i.substring(1)+
              '</i>: '+_pp[i]+'<br/>';return s;}
    }
    var car_1 = new Car( 'honda', 'accord', 2004 );
    document.write( car_1.get('make') );
    document.write( car_1 );
     
    </script>

    Could anyone tell me the significance of this approach?

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Location
    Rosario, Argentina
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is a sample class in javascript.. really ugly code.. hard to read, hard to understand... why you need it?

  3. #3
    SitePoint Enthusiast Grayson's Avatar
    Join Date
    Jun 2008
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Adjusted the spacing. It looks like make, model, and year are being set to Car::_pp['_make'] etc...
    The tostring looks pretty badass when i tried the example. What's the point of doing var _pp instead of just doing this.make?
    Code javascript:
    <script type='text/javascript'>
     
     
    function Car( make, model, year )
    {
          var _pp=
    	  {
    		'_make'   :   make,
    		'_model'  :   model,
    		'_year'   :   year
          }
          this.get=function(pp)
    	  {
    		return eval('_pp._'+pp);
    	  }
          this.toString=function()
    	  {
    	    var s='<br/><b>Car</b><br/>';
            for(i in _pp)
    		  s+='<i>'+i.substring(1)+'</i>: '+_pp[i]+'<br/>';
    		return s;
    	  }
    }
     
     
     
    var car_1 = new Car( 'honda', 'accord', 2004 );
    document.write( car_1.get('make') );
    document.write( car_1 );
     
     
    </script>

    Output of document.write( car_1.get('make') ) is 'honda', but the output of document.write( car_1._pp._make ) is 'undefined' ?
    Output of document.write( car_1 ) is :
    Code:
    Car
    make : honda
    model : accord
    year : 2004

  4. #4
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The contents of this block is private to the function - think of it as private variables in VB/C# - Essentially _pp is a private 'class' within the Car 'class'. Because it's private, it's properties cannot be accessed outside of the class that encapsulates it, therefore the encapsulating class - 'Car' - needs to define a method to access the properties of _pp ('make', 'model' and 'year'). What defines it as private? The var keyword. It defines _pp as a *variable* essentally (although it's a 'class') - think of it as a complex variable if you like. If you were to change the var _pp = keyword to this._pp - you'd see that you could now do:

    document.write(car_1._pp._make) and it would return 'honda'.

    I should stress the importance of the 'this' keyword - it refers to the current function - in this case the instance of 'Car', 'car_1'. Think of it as the equivilent of 'Me' in VB.NET (if that's any help).

  5. #5
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The idea is to have a private property. It is a security measure so that properties can't be changed.

    Code JavaScript:
    car_1.make = 'Ford' //doesn't work

  6. #6
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's no need for using eval though. The eval-line could be written as:
    Code javascript:
    return _pp["_" + pp];


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
  •