Create Dynamic Objects with the 'new' keyword

Published on: March 18, 2017

Welcome to the 39th Easy JavaScript tutorial, part of EasyProgramming.net. Lets continue to look at JavaScript objects by creating our own custom objects using the new keyword. Let's also utilize the this keyword.

This tutorial also covers the use of a constructor, can be considered an object template. 

Syntax of an object constructor:

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

Creating and accessing new object:

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

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

    item1.name1

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

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

Resources:

Comments: