price quote

Register/ Signup

Login

Essay Samples

Essay Topics

How to Write Guide

How to Write An Essay

Essay Help

Research Papers

Research Paper Topics

Book Review

Blog

JavaScript Tutorial

why choose us

email us

Agent Signup
Free PR Checker
home buttonour services buttonplace order buttonwhy choose us buttoncontact us button
place order to get custom essay, custom term paper, custom research paper, thesis, disserationsign up to receive free and custom essays, term paper, research paper, thesispromise to provide quality essays, term paper, research paper, thesisearn money affiliate agent programfree sample essays, term paper, research paper
Our Prices

Flash Order: (8 Hours)

$39/page Order Now

Rush Order: (2 Days)

$29/page Order Now

Normal Order: (5 Days)

$25/page Order Now

Economic Order: (14 Days)

$20/page Order Now

Super Saver: (2 Months)

$15/page Order Now

Order Now
Limited Time Discount
Our Services
Essay Writing Services
Essay Help
Custom Essay & Term Paper Writing
Research Dissertation
Admission Documents
Editing & Proof Reading
Computer Applications
Web Design
How to Write Essays
How to an Write an Essay
How to Write a Book Report
How to Write a Thesis
Free Essay Samples
Abortion
Book Review: Get to Work
Film Review-Sicko
Sigmund Freud
Essays Topics
Application Essay
Argumentative Essay
Love Essay
Persuasive Essay
Buying Custom Essays
Buy Essay
Buy a Term Paper
Buy a Research Paper
Buy a custom essay

JavaScript Create Your Own Objects

JavaScript Objects

  • JavaScript has several built-in objects, like String, Date, Array, and more. In addition to these built-in objects, you can also create your own.
  • An object is just a special kind of data, with a collection of properties and methods.

Person Object:

  • Properties are: name, height, weight, age, skin tone, eye color, etc.
  • Methods are: eat(), sleep(), work(), play(), etc.

Accessing object properties or methods:

objName.propName

Add properties to an object by simply giving it a value:

personObj.firstname="John";
personObj.lastname="Doe";
personObj.age=30;
personObj.eyecolor="blue";
document.write(personObj.firstname);
John

Call an Object Methods

objName.methodName()

Creating Your Own Objects

There are different ways to create a new object:
1. Create a direct instance of an object

personObj=new Object();
personObj.firstname="John";
personObj.lastname="Doe";
personObj.age=50;
personObj.eyecolor="blue";

Adding a method to the personObj following code adds a method called eat() to the personObj:

personObj.eat=eat;

2. Create a template of an object
The template defines the structure of an object:

function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}

This template is just a function. This is instance of current object.
Create a new instance using the template:

myFather=new person("John","Doe",50,"blue");
myMother=new person("Sally","Rally",48,"green");

Add Methods inside the template:

function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
this.newlastname=newlastname;
}
  • Methods are just functions attached to objects.

newlastname() function:

function newlastname(new_lastname){this.lastname=new_lastname;}

You can write: myMother.newlastname("Doe").