How to Create Custom Methods

Published on: March 25, 2017

Hey there! Welcome to the 40th Easy JavaScript tutorial, part of EasyProgramming.net. Let's continue to look at JavaScript objects by creating our own custom methods and adding on to what we've learned so far.

As we've seen in the last tutorial, we'll be looking more at an object constructor. We'll be creating a custom object and adding a custom method and then triggering that method to do something with the data within the object. Confused? Don't be! Just watch the tutorial below.

Syntax of an object constructor:

var person = function(param1, param1, param3){
        this.name1 = param1,
        this.name2 = param2,
        this.name3 = param3,
        this.method1 = function(){
            //write what method1 does
        }
    };
  

Creating and accessing new method:

    var item1 = new person(arg1, arg2, arg3);
    var item2 = new person(arg1, arg2, arg3);
    item1.method1()

To fork the fiddle and follow along: https://jsfiddle.net/easyjs/57xu3Lnj/

 

Remember to checkout the Resources section below for associated downloadable content, JSFiddle links, and other resources. Watch the video and follow along!

Resources:



Comments: