Web Engineering
Code 4 Fun
Introduction to JavaScript Part 3
Lecture: 10
Lecturer: Ahmad Gul “Ahmadi”– Code 4 Fun-
Zabul 1
Contents
JavaScript
Objects
2
Object
JavaScript is an Object Oriented
A programming language can be called object-oriented
s Programming (OOP) language.
if it provides four basic capabilities to developers:
Encapsulation: the capability to store related
information, whether data or methods, together
in an object.
Aggregation: the capability to store one object inside
another object.
Inheritance: the capability of a class to rely upon
another class (or number of classes) for some of its
properties and methods.
Polymorphism: the capability to write one function or
method that works in a variety of different ways.
3
Object
s
Objects are composed of attributes. If an attribute
contains a function, it is considered to be a
method of the object, otherwise the attribute is
considered a property
4
Object
Properties
Object properties can be any of the three primitive
data types, or any of the abstract data types, such as
another object.
Object properties are usually variables that are used
internally in the object's methods, but can also be
globally visible variables that are used throughout
the page.
The syntax for adding a property to an object is:
objectName.objectProperty = propertyValue; 5
Object Properties
Example
For example: The following code gets the document title
using the "title" property of the document object.
var str = document.title;
6
Object Methods
Methods are the functions that let the object do
something or let something be done
to it.
method: A function is a standalone unit of statements.
There is a small difference between a function and a
A method is attached to an object and can be
referenced by the this keyword.
7
User-Defined
Objects
All user-defined objects and built-in objects are
descendants of an object called
Object.
8
The new
Operator
The new operator is used to create an instance of
an object.
To create an object, the new operator is followed by
the constructor method.
9
The new
Operator
In the following example, the constructor methods
are Object(), Array(), and Date().
These constructors are built-in JavaScript functions.
var employee = new Object();
var books = new Array("C++", "Perl",
"Java"); var day = new Date("August 15,
1947");
10
The Object ( )
Constructor
A constructor is a function that creates and initializes an object.
JavaScript provides a special constructor function called
The return value of the Object() constructor is
Object() to build the object.
assigned to a variable.
The variable contains a reference to the new
object.
The properties assigned to the object are not variables and
are not defined with the var keyword.
11
The Object ( ) Constructor
Example1
Example: Try the following example; it demonstrates how to create
an Object.
12
The Object Example2
This example demonstrates how to create an object
with a User- Defined Function. Here this keyword is
used to refer to the object that has been passed to a
function.
13
Defining Methods for an
Object
The previous examples demonstrate how the
constructor creates the object and assigns
properties.
But we need to complete the definition of an
object by assigning methods to it.
14
Defining Methods for an
Object
15
The ‘with’ Keyword
The ‘with’ keyword is used as a kind of shorthand
for referencing an object's properties or
methods.
The object specified as an argument to with
becomes the default object for the duration of the
block that follows.
The properties and methods for the object can be
used without naming the object.
16
The ‘with’
Keyword
17
Thanks
!
?
18