27. Cteate Your Own Objects with JavaScript

Create Your Own Objects with JavaScript

JavaScript Objects
Creating Your Own Objects

Objects are used to organize information

JavaScript Objects

An oibject is just a special kind of data, a collection of properties and methods.

Ex:

create an object that models a person.
"properties are the values assosiated with the object"
The persons properties incluede name, height,weight,age,skin tone, eye color, and so on.
All persons have these properties, but the values of those properties differ from person to person.
Objects also have methods.
"methods are the actions that can be performed on object."
The person's methods could be eat(),sleep(),work(),play(), and so on.

Properties

objName.propName

you can add a property to an object by simply giving it a value.

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

Methods

objNmae.methodName()
perameters required for the method can be passed between the parantheses.

personObj.sleep();
if the sleep() method accepts a parameter for the number of hours, it could be called like this:
personObj.sleep(8);

Creating Your Own Object
2 ways to create a new object
you can create a direct instance of an object
or
you can create a template of an object.

Create a Direct Instance of an Object
personObj=new Object();
personObj.firstname="John";
personObj.lastname="Doe";
personObj.age=30;
personObj.eyecolor="blue";
adding a method to the personObj is also simple.
personObj.eat=eat;

<html>
<body>
<script type="text/javascript">
personObj=new Object();
personObj.firstname="John";
personObj.lastname="Doe";
personObj.age=50;
personObj.eyecolor="blue";
document.write(personObj.firstname+"is"+personObj.age+"years old.");
</script>
</body>
</html>

Create a Template of an Object

The template defines the structure of an object so that you can more easily create multiple instances of that object

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

template is just a function.
It is also called a constructor.
you add the properties and methods that will belong to each subsequent instance of the object.
when you use person as a constructor for more than one object, you must include the "this" keyword.
Javascript uses "this" to assign the properties to the specific object created with the "new" keyword.

<html>
<body>
<script type="text/javascript">
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}
myFather=new person("Jhon","Doe",50,"blue");
document.write(myFather.firstname+"is"+myFaver.age+"years old.");
</script>
</body>
</html>

after you have template, you can create new instances of the object, like this:

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

you can also add some methods to the person object. this is also done inside the template:

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

Note that methods are just functions attached to onjects.
then you will have to write the newlastname() function:

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

The newlastname() function defines the person's new last name and assigns that to the person.
Javascript knows which person you're talking about by using "this"
so, now you can write: myMother.newlastname("Doe").

No comments:

My Favorite Books

  • C and Data Structures by Ashok N. kamthane
  • Web Technologies by A. A. Puntambekar
  • Learn HTML and CSS with W3Schools
  • Learn JavaScript and Ajax with W3Schools
  • HTML5 Black Book: Covers Css3, Javascript,XML, XHTML, Ajax, PHP And Jquery
  • HTML5 Application Development Fundamentals: MTA Exam 98-375
  • .NET 4.0 Programming 6-In-1, Black Book
  • SQL Server 2008 R2 Black Book
  • Asp.net 4.0 Projects Covers: net 3.5 And .net 4.0 Codes, Black Book
  • UNIX Shell Programming 1 Edition by Yashavant Kanetkar
  • UNIX and Shell Programming 1 Edition by Richard F. Gilberg, Behrouz A. Forouzan
  • Computer Networks by Andrew S. Tanenbaum
  • Multiple Choice questions in computer science by Timothy J Williams