Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript big time problem understanding object

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #153644
    javascriptnewbie
    Participant

    alright now that i’ve understood how to make objects..

    book = new Object();
    variableName.color=”green”;
    variableName.size=”10″;

    What’s the point..
    What to do with them?

    I know i can use it to print variableName.color value onscreen, but what else….

    also what are methods and what’s the use of them?

    big thanks to everyone !

    #153648
    __
    Participant

    What’s the point.. What to do with them?

    They’re particularly useful for grouping/scoping your code. But really, you might as well ask “what to do?” — objects are suitable for lots of things in javascript; it’s a pretty open-ended question.

    also what are methods and what’s the use of them?

    methods are functions that belong to an object.

    var book = {};
    book.color = "green";
    book.size = 10;
    book.describe = function(){
        var description = "This book is " +  this.color +
            " and is " + this.size + " pages long.";
        return description;
    }
    
    #153649
    Alen
    Participant

    Objects have properties and methods.

    Properties are just values. And methods are just functions that do something with the values.

    As you can see in this example:

    obj = {
      firstName: "Alen",
      lastName: "Mofo",
      fullName: function(){
        alert(this.firstName + " " + this.lastName);
      }
    }
    obj.fullName(); //Prints "Alen Mofo"
    

    firstName and lastName is a property. fullName is a method.

    #153650
    Alen
    Participant
Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.