JavaScript (Part 6)- Objects
JavaScript (Part 6)- Objects
Ullrich Hustadt
1 (User-defined) Objects
Object Literals
Object Constructors
Prototype Property
Public and Private Static Variables
for/in-loops
Inheritance
Classes as Syntactic Sugar
2 Pre-defined Objects
Regular Expressions
String
Date
3 Further Reading
Object Literals
• JavaScript is an object-oriented language, but one without classes
• Instead of defining a class,
we can simply state an object literal
{ memberName1 : value1 , memberName2 : value2 , ...}
memberName1, memberName2, . . . are member names
value1, value2, . . . are member values (expressions)
• Terminology:
member value is function ; method
member value is some other value ; property
{
age : (30 + 2) ,
gender : ' male ' ,
nme : { first : ' Ben ' , last : ' Budd ' } ,
interests : [ ' music ' , ' skiing '] ,
hello : function () { return ' Hi ! I \ ' m ' + this . nme . first }
};
COMP519 Web Programming Lecture 15 Slide L15 – 2
(User-defined) Objects Object Literals
Object Literals
• Member values can be accessed using dot notation or bracket notation
var person1 = {
age : (30 + 2) ,
gender : ' male ' ,
nme : { first : ' Ben ' , last : ' Budd ' } ,
interests : [ ' music ' , ' skiing '] ,
hello : function () { return ' Hi ! I \ ' m ' + this . nme . first }
};
person1 . age --> 32 // dot notation
person1 [ ' gender '] --> ' male ' // bracket notation
person1 . nme . first --> ' Ben '
person1 [ ' nme '][ ' last '] --> ' Budd '
Object Literals
var person1 = {
...
nme : { first : ' Ben ' , last : ' Budd ' } ,
hello : function () { return ' Hi ! I \ ' m ' + this . nme . first }
};
person1 . hello () --> " Hi ! I ' m Ben "
person1 [ ' hello ']() --> " Hi ! I ' m Ben "
• Every part of a JavaScript program is executed in a particular
execution context
• Every execution context offers a keyword this
as a way of referring to itself
• In person1.hello() the execution context of hello() is person1
; this.nme.first is person1.nme.first
Object Literals
var nme = { first : ' Adam ' , last : ' Alby ' }
var person1 = {
nme : { first : 'Ben ' , last : ' Budd ' } ,
hello : function () { return 'I \ ' m ' + this . nme . first } ,
Object Literals
var nme = { first : ' Adam ' , last : ' Alby ' }
var person1 = {
nme : { first : 'Ben ' , last : ' Budd ' } ,
hello : function () { return 'I \ ' m ' + this . nme . first } ,
full1 : this . nme . first + " " + this . nme . last ,
full2 : nme . first + " " + nme . last ,
greet : function () { return 'I \ ' m ' + nme . first }
}
• In the construction of the object literal itself, this does not refer to
person1 but its execution context (the window object)
; none of nme.first, nme.last, this.nme.first, and
this.nme.last refers to properties of this object literal
• In person1.greet() the execution context of greet() is person1
; but nme.first does not refer to person1.nme.first
• Do not think of an object literal as a block of statements (and of
property/value pairs as assignments within that block)
Object Constructors
• JavaScript is an object-oriented language, but one without classes
// public method
this . method1 = function () {
// ( use of a ) public variable must be preceded by ` this '
// ( use of a ) private variable must NOT be preceded by ` this '
return ' m1 [ prop1 = ' + this . prop1 + ' prop2 = ' + prop2 + '] ' }
// private method
var method2 = function () {
return ' m2 [] ' }
// public method
this . method3 = function () {
// ( call of a ) public method must be preceded by ` this '
// ( call of a ) private method must NOT be preceded by ` this '
return ' m3 [ ' + this . method1 () + ' ' + method2 () + '] ' }
}
obj1 = new SomeObj () // creates a new object
obj1 . prop1 = 'A '
obj1 . prop2 = undefined
obj1 . method1 () = ' m1 [ prop1 = A prop2 = B ] '
obj1 . method2 () --> TypeError : obj . method2 is not a function
obj1 . method3 () = ' m3 [ m1 [ prop1 = A prop2 = B ] m2 []] '
COMP519 Web Programming Lecture 15 Slide L15 – 8
(User-defined) Objects Object Constructors
// private method
var method4 = function () {
// ( use of a ) public variable must be preceded by ' that '
// ( use of a ) private variable must NOT be preceded by ' that '
return ' m4 [ prop1 = ' + that . prop1 + ' prop2 = ' + prop2 + '] ' }
// private method
var method6 = function () {
// ( call of a ) public method must be preceded by ' that '
// ( call of a ) private method must NOT be preceded by ' that '
return ' m6 [ ' + that . method1 () + ' ' + method4 () + '] ' }
for/in-loop
• The for/in-loop allows us to go through the properties of an object
for ( var in object ) { statements }
• Within the loop we can use object[var] to access the value of the
property var
var person1 = { age : 32 ,
gender : ' male ' ,
name : ' Bob Smith '
}
for ( prop in person1 ) {
console . log ( ' person1 [ ' + prop + '] has value '
+ person1 [ prop ]);
}
person1 [ gender ] has value male
person1 [ name ] has value Bob Smith
person1 [ age ] has value 32
Inheritance
• The prototype property can also be used to establish
an inheritance relationship between objects
function Rectangle ( width , height ) {
this . width = width
this . height = height
this . type = ' Rectangle '
this . area = function () { return this . width * this . height }
}
/^\ s *[ a - z ]+\ s * $ / matches " abc " , " x " , " ab "
but not "0 abc " , " abc1 " , " AB " , ""
/(\ w +)(\ d ).\2\1/ matches " ab1Z1ab " , " abc0 0 c " , " _1 |1 _9 "
but not " ab1Z1ca " , "1 Z1 "
/((\ d +)\ $ )\1\2 $ / matches "10 $10$10 " , " A9$9$9 "
but not "9 $8$9 " , "9 $9$9A "