www.webdeveloper.com
Results 1 to 5 of 5

Thread: Classes and Function Prototyping

  1. #1
    Join Date
    Aug 2008
    Posts
    5

    Classes and Function Prototyping

    Hello all. I'm trying to work my way around creation of Objects and prototyping. First time I'm really trying to OOP route, and not resorting to simply using basic function.

    Below is my class definition and prototype.
    PHP Code:
    function user(){
        
    //Declaring Variables
        
    this.alias "Sample Name";
    }
    user.prototype.getAlias getAlias;
    user.prototype.setAlias setAlias;
    function 
    getAlias(){
        return 
    this.alias;    
    }
    function 
    setAlias = function(val){
        
    this.alias val;

    Next I create a user array populated by xml. For each user I'm looking to instantiate the object, then proceed by calling the 'setAlias' method.
    PHP Code:
    UserList = new Array();
    <
    xsl:for-each select="UserList/User">
        
    UserList[<xsl:value-of select="position()-1"/>] = new user();
        var 
    tmpFc FcList[<xsl:value-of select="position()-1"/>];
        
    tmpFc.setAlias "<xsl:value-of select="Alias"/>";
    </
    xsl:for-each>
    function 
    TestObject(){
        
    alert(UserList[0].getAlias);

    When I make a call to the 'Test Object' function to see if the property is being correctly set. the function declartion is returned as a string. ie: the alert displays the following:

    PHP Code:
    function(){
        return 
    this.alias;    

    I would expect to get the property returned not the function declaration. I'm pretty certain I'm making a simple error, but I'm lost. Any help is appreciated at this point.

    Thank you.

  2. #2
    Join Date
    Aug 2007
    Posts
    151
    Code:
    function TestObject(){
        alert(UserList[0].getAlias());
    }
    getAlias is method, not property

  3. #3
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    Some suggestions.

    Start class names with uppercase letters to follow convention
    Code:
    function User(){ 
        //Declaring Variables 
        this.alias = "Sample Name"; 
    }
    You don't need to create global functions and later assign them. Instead of
    Code:
    user.prototype.getAlias = getAlias; 
    function getAlias(){ 
        return this.alias;     
    }
    do
    Code:
    User.prototype.getAlias = function(){return this.alias;}

  4. #4
    Join Date
    Aug 2007
    Posts
    3,767
    And if you have a lot of prototypes assign an object to the prototype.
    Code:
    User.prototype = {
    getAlias:function() {
    return this.alias;
    },
    setAlias:function(a) {
    this.alias = a;
    }
    }

  5. #5
    Join Date
    Aug 2008
    Posts
    5
    Thx for the quick feedback. the "()" after the method call did the trick. the additional information on multiple method declaration and adhering to standards is much appreciated.

    Cheers

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