www.webdeveloper.com
Results 1 to 2 of 2

Thread: Help with objects in Javascrit

  1. #1
    Join Date
    Dec 2010
    Posts
    4

    Help with objects in Javascrit

    Code:
     //Define the class
        
        class author{
        
        function author(a:String,b:String,c:int)
        
           {
           
        this.name = a;
        
           this.lastname = b;
        
           this.age = c;
        
           } 
        
        //Declaring the variables
        
        var name:String;
        
        var lastname:String;
        
        var age:int;
        
        }
        
        //I make a new object
        
        newBook = new author("J.R.","Tolkien",60); 
        
        
        //I make other new object
        
        newBook = new author("Agatha","Christie",42);

    in this code the variable "newBook" to save the whole object, returns to save only the last one that was created...


    Any help???

  2. #2
    Join Date
    Mar 2009
    Posts
    468
    Javascript doesn't really have classes in the general meaning of the term.

    And there are various ways to create an object. Might I suggest the following for the example you cite:

    function createNewBookObj(firstName, lastName, Age){
    var x = {}; //create a generic object
    x,name = firstName; //add the property name to the object and set the value to the argument
    x.lastname= lastName; // repeat for the second property
    x.age = Age; // repeat for the third property
    return x; // return the entire created object
    }

    So, to create an object holding the information listed in your code you would

    var myObj = createNewBookObj("J.R.", "Tolkien", "60");

    You can access the data within by referring to it as( for example):
    alert(myObj.age) which would give an alert box indicating 60

    I hope this helps.

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