Home

How to Add Methods to an Object in JavaScript for HTML5 and CSS3 Programming

|
|  Updated:  
2016-03-26 13:15:23
HTML5 and CSS3 All-in-One For Dummies
Explore Book
Buy On Amazon

Objects in JavaScript have other characteristics besides properties. They can also have methods. A method is simply a function attached to an object. To see what this means, take a look at this example:

 //create the critter
 //from addingMethods.html
 var critter = new Object();
 //add some properties
 critter.name = "Milo";
 critter.age = 5;
 //create a method
 critter.talk = function(){
 msg = "Hi! my name is " + this.name;
 msg += " and I’m " + this.age;
 alert(msg);
 } // end method
 // call the talk method
 critter.talk();

In addition to properties, the new critter has a talk() method. If a property describes a characteristic of an object, a method describes something the object can do.

image0.jpg

Here's how it works:

  1. Build an object with whatever properties you need.

    Begin by building an object and giving it some properties.

  2. Define a method much like a property.

    In fact, methods are properties in JavaScript, but don't worry too much about that; it'll make your head explode.

  3. You can assign a prebuilt function to a method.

    If you created a function that you want to use as a method, you can simply assign it.

  4. You can also create an anonymous function.

    More often, you'll want to create your method right there as you define the object. You can create a function immediately with the function(){ syntax.

  5. The this keyword refers to the current object.

    Inside the function, you may want to access the properties of the object. this.name refers to the name property of the current object.

  6. You can then refer to the method directly.

    After you define an object with a method, you can invoke it. For example, if the critter object has a talk method, use critter.talk() to invoke this method.

About This Article

This article is from the book: 

About the book author:

Andy Harris earned a degree in Special Education from Indiana University/Purdue University–Indianapolis (IUPUI). He taught young adults with severe disabilities for several years. He also taught himself enough computer programming to support his teaching habit with freelance programming.
Those were the exciting days when computers started to have hard drives, and some computers connected to each other with arcane protocols. He taught programming in those days because it was fun.
Eventually, Andy decided to teach computer science full time, and he still teaches at IUPUI. He lectures in the applied computing program and runs the streaming media lab. He also teaches classes in whatever programming language is in demand at the time. He has developed a large number of online video-based courses and international distance education projects.
Andy has written several books on various computing topics and languages including Java, C#, mobile computing, JavaScript, and PHP/MySQL.
Andy welcomes comments and suggestions about his books. He can be reached at [email protected].