Animations
API
Arrays
Async
Basics
Challenges
Classes
Console
Dates
Debugging
DOM Elements
DOM Methods
DOM Properties
Event Listeners
Flow Control
Forms
Functions
Global Functions
JSON
Keywords
Libraries (3rd party)
Math
Modules
Objects
Snippets
String
Types
Widgets
Window Object
The standard way to create an object with default values is to use an object constructor function. In this example, you must
pass a series of arguments to the object constructor to instantiate it. This is also known as a object Prototype
.
function Person(first, last, age, eye)
{
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
Here's a good example of creating multiple "persons":
let father = new Person("John", "Doe", 50, "blue");
let mother = new Person("Sally", "Rally", 48, "green");
let msg = "Father name is " + myFather.firstName + " " + myFather.lastName + "." + "<br />";
msg += "Mother name is " + myMother.firstName + " " + myMother.lastName + "." + "<br />";
document.getElementById("demo1").innerHTML = msg;;