Objects in Javascipt

You can make objects in JavaScript. Here’s some code that does just that in a couple of different ways.

The Way of the Object Literal:
Using object literals uses the smallest amount of characters. (Thanks Nick!)

// Create an object variable called "jake"
var jake = {
    age: 26,
    height: "6Ft",
    start_coding: function () {
        this.typing = true;
        this.thinking = true;
    }
}
// Attempt to duplicate object
var dave = jake
// Call a method
jake.start_coding();
// Jake is now coding.
// AND SO IS DAVE!

Rather then duplicating the object the code above creates two references to the same object. Jake and Dave are now two names for the same thing.

The Middle Way:
This way, you create a blank object and then assign new parameters and methods to it. To create a new version of the object you would have to make a copy of an existing one.

// Create a new object
jake = new Object();
// Assign some properties
jake.age = 26;
jake.height = "6Ft";
// Create a method
jake.start_coding = function(){
    this.typing = true;
    this.thinking = true;
}
// Attempt to duplicate the object
var dave = jake
// Call a method
jake.start_coding();
// Jake is now coding.
// AND SO IS DAVE!

As in the previous example Jake and Dave are now both names for the same object.

The Way of the Object Constructor:
An object constructor is used to describe a class of objects, similar to how other languages implement objects/classes. First you design your class by making a constructor function, then it is used to construct each instance of your object. Methods are added by defining a function outside of the constructor and then linking the two together (see line 5).

// Define the object by creating a constructor function
function person = (name, age){
    this.name = name;
    this.age   = age;
    this.start_coding = write_code; // link the method to the object
}
// Define a method
function write_code(){
    this.thinking = true;
    this.typing = true;
}
// Create two new people
var jake = new person('jake', 26);
var dave = new person('dave', 25);
// Call a function
jake.start_coding();
// Jake will now be coding. Dave won't be.

As you can see, in this last example I can create two fresh objects of the same class using a constructor.

Note: I’ve just realised that it’s been a while since I’ve actually posted any code. I’ve been too busy slagging of Microsoft and Filemaker. Sorry about that.

Advertisements

About Mr Chimp

I make music, draw pictures, browse the internet, programme, and make sweet, sweet cups of tea until the early hours.
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

2 Responses to Objects in Javascipt

  1. Skilldrick says:

    Even simpler than the first way is object literals:

    
    var jake = {
      age: 26,
      height: "6Ft", //ish
      write_code: function () {
        this.typing = true;
        this.thinking = true;
      }
    }
    

    which gives you an equivalent object.

  2. Mr Chimp says:

    Good point. I’ve edited the post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s