Objects

Objects are seen all around us everyday such as a television or books. JavaScript has 3 types of objects:


An object has propeties and methods. We can create an object with an object constructor and the keyword new or with the object literals.

First of all we are going to look at creating objects using the keyword new.


SyntaxExample

nameOfObject=new Object();

<script>
var television=new Object()
<s/cript>



Property

An object can have several properties. A television would have the properties size and model.

The syntax for a property would be the name of the object followed by an operator, which is a dot then the property name.


SyntaxExample

nameOfObject.propertyName

<script>
television.size;
television.model;
</script>



Value

A property is a variable so it must have a value.

SyntaxExample

objectName.propertyName=propertyValue

<script>
television.size=24;
television.model="sony";
</script>


Method

Methods are used to perform tasks. They are action words (verbs). For example a television can be switched on or off. The syntax for a function would be the object name followed by the dot then the name of the method with parentheses. Inside the parentheses, a method can have zero or more arguments.


SyntaxExample

objectName.methodName();

<script>
television.turnOn();
television.turnOff();
</script>


Constructor

An object constructor is a function. The function name would be the object name. The first letter of the object name starts with a capital letter, which helps you recognise an object function from a normal function. The object function has properties and methods. Let's have a look at a function called Student and its properties .

The keyword this is used to represent the current object.

Example 1: Student Constructor


<script>
function Student(name,id)
{
this.name = name;
this.id = id;
}
<script>

Create two student objects using the keyword new

var s1 = new Student("Tim",123);
var s2 = new Student("Jane",234);


Now we have created the 2 objects, we need to display the outcome on the screen.

<script>
//student1
document.write("my name is " + s1.name +"<br>")
document.write("my id is "+ s1.id +"<br>")

//student2
document.write("my name is " + s2.name +"<br>")
document.write("my id is " + s2.id + "<br>")
</script>

Displays



Create a Method

We are going to create a method called course and add it to our Student constructor. There are two ways to add the function (see below example 1 and 2). to the constructor.

<script>
// create course function
function course(subject)
{
this.subject=subject;
return this.subject;
}
</script>


Example 1:

<script>
function Student(name,id)
{
this.name = name;
this.id = id;

//add the function course
this.course=course; }
<script>

Example 2:

<script>
function Student(name,id)
{
this.name = name;
this.id = id;

//add the function course
this.course=function(subject){
this.subject=subject;
return this.subject;}
}
<script>


Now we have created the method we need to display the outcome on the screen.

<script>
//student1
document.write("Tim studies " +
s1.course("Biology"))

//student2
document.write("Jane studies " +
s2.course("Maths"))
</script>

Displays




Example 2: Employee Constructor

We are going to create a constructor called Employee. Inside the parentheses, we are going to have: name, nI and rate.


<script>
function Employee(name,nI,rate)
{
this.name = name;
this.nI = nI;
this.rate=rate;
}
<script>

Create an employee object using the keyword new

var emp=new Employee("Tim",345,9);

Now we have created an object, we need to display the outcome on the screen.

<script>

document.write("Name:" + emp.name +"<br>")
document.write("NI:"+ emp.nI +"<br>")
document.write("Rate:£" + emp.rate +"<br>")

</script>

Displays



Create a Method

We are going to create a method called earned and add it to our Employee object.

<script>
function earned(hours)
{
this.hours=hours;
return this.hours * this.rate;
}
</script>

<script>
function Employee(name,nI,rate)
{
this.name = name;
this.ni= nI;
this.rate=rate;
this.earned=earned;
}
<script>


Now we need to display the outcome on the screen.

<script>
var emp1=new Employee("Tim",345,9)
var salary= emp1.earned(30)
document.write("Tim's Salary: £" + salary)
</script>

Displays




Object Literals

Object literals is a short way of creating an object.

Syntax

object_name={property1:value1}


Example

<script>
//create an object
var employee={name:"Lilly",nI:3456,rate:30}

//access the properties
document.write("Name: "+ employee.name +"<br>")
document.write("nI: "+ employee.id +"<br>")
document.write("rate: "+ employee.rate +"<br>")
<script>

Displays



Create a Method

The method course goes inside the curly brackets after the rate value.

Note:The function's curly brackets.

var employee={name:"Lilly",nI:3456,rate:30,
earned:function(hours){
this.hours=hours;
return this.hours*this.rate;}
}

//access the method
document.write("Salary:£"+ employee.earned(30)
+"<br>")
<script>

Displays




Prototype

Prototype is a property, which allows you to add properties and methods to an object.

Syntax

object.prototype.propertyName/methodName= value;


Add a Property

<script>
//add a property called courseFee
Student.prototype.courseFee=220;

var student1= new Student("sara",3456)

document.write("Course Fee: £" +
student1.courseFee);
<script>

Displays




Add a Method

<script>
//add a method called welcome
Student.prototype.welcome=
function(){return "Welcome"+this.name};

var student1= new Student("Sara",3456);

document.write(student1.welcome());
<script>

Displays




Inheritance

First of all, we are going to create a constructor called Account() and add a method called addAmount()



<script>

//constructor
function Account()
{
this.balance=0;
this.addAmount=addAmount
}

//method
function addAmount(amount)
{
this.amount=amount;
balance+=this.amount;
return "Confirmed
£"+ this.amount+" added";
}

<script>
<script>

//create an object
var acc=new Account;
document.write(acc.addAmount(300))
document.write("New balance:£"+
acc.balance)
</script>

Displays




Now we want the Employee and Student instances to inherit the properties and methods of Account().


<script>
//inherit the properties and methods of Account()
Employee.prototype=new Account();
Student.prototype=new Account();

// create objects
var emp22=new Employee("Jane",5632,12);
var s=new Student("Kane",8987,12);

//access to Account() properties and methods
document.write("Add employee wk1 salary:"+emp22.addAmount(350))
document.write(<br>)
document.write("Add employee wk2 salary"+emp22.addAmount(350))
document.write(<br>)
document.write("Get employee account Balance:£" + emp22.balance)

document.write(<br>)
document.write("Add student course fee:" s.addAmount(325))
document.write("Get balance:£"+s.balance)
</script>


Displays





Now do the Exercises!