9B Gentle Introduction to Objects
9B Gentle Introduction to Objects
Cosmin E. Oancea
[email protected]
Nesting objects;
TRUE
FALSE
TRUE
FALSE
f u n c t i o n dummyPrefixSum ( a r r a y ) {
let result = [ ] ;
for ( l e t i =0; i < array . length ; i = i +1) {
The complexity of the dummyPrefixSum in terms of the length n of the input array is
(a) O( 1 )
(b) O( log(n) )
(c) O( n )
(d) O( n2 )
(e) O( 2n )
Please explain why.
Arrays and Strings
to create a slice of an array arr that contains the elements from position beg up
until and excluding position end use arr.slice(beg, end)
to split a string txt that consists of multiple lines, into an array of (string) lines, use
txt.split("\n")
similarly, to split a string into an array of strings using space as separator, one can
use txt.split(" ")
joining the (string) elements of an array arr into one big string (with space as
separator between elements) can be achieved with arr.join(" ")
keeping only letter, digits, spaces and common punctuation and eliminating all
other (wired) symbols from a string:
s t r . r e p l a c e ( / [ ˆ a−zA−Z0 − 9 .; , ’ − ’ ] / g , ” ” )
Objects
l e t g r e a t m o vi e = { t i t l e : ” Jaws ”
, ye a r : 1975
, mins : 124
};
l e t p o s i t i o n = { x : 400 , y : 20 } ;
There are other ways to create objects as well (see next slide).
co n s o l e . l o g ( g r e a t m o vi e . t i t l e ) ;
p o s i t i o n . x += 5 0 ;
Nested Objects
monster . o l d p o s i t i o n s [ 1 ] . x −= 1 0 ;
monster . o l d p o s i t i o n s [ 1 ] . y += 5 ;
co n s o l e . l o g ( monster . o l d p o s i t i o n s [ 1 ] ) ;
Object Oriented Programming
Object-oriented programming (OOP) is a programming paradigm
based on the concept of “objects”, which can contain data and
code: data in the form of fields (often known as attributes or
properties), and code, in the form of procedures (often known as
methods).
The core idea is to divide programs into smaller pieces and make
each piece responsible for managing its own state.
c o n s t r u c t o r ( name ) {
this.name = name ;
}
getName ( ) { / / t h i s i s a method
r e t u r n ”name i s : ” + this.name ;
}
}
The constructor is the code that gets executed when creating a new object.
Meaning: when you call new User(’John Snow’); this essentially executes
(calls) the constructor of the User class with ’John Snow’ as its argument.
In JavaScript there can be only one constructor per class.
Inheritance
A class can “extend” or “inherit” the functionality of another class.
c l a s s S t u d e n t extends User {
grad courses; / / new f i e l d / p r o p e r t y
c o n s t r u c t o r ( name , c o u r s e s ) {
super(name); / / c a l l s t h e c o n s t r u c t o r o f U s e r
t h i s . g r a d co u r s e s = courses ;
}
addGradCourse(course) {
t h i s . g r a d c o u r s e s . push ( co u r s e ) ;
}
}
l e t s t u d = new S t u d e n t ( ’ H a r r y Po t t e r ’ , [ ’ p o t i o n s ’ ] ) ;
stud.getName(); / / i n h e r i t e d fr o m U s e r
stud.name; / / i n h e r i t e d fr o m U s e r
stud.addGradCourse(”intro to IT”); / / new f u n c t i o n a l i t y
stud.grad courses; / / new f u n c t i o n a l i t y
Possible Uses of Classes/Objects
c o n s t r u c t o r ( nm , c r s ) {
t h i s . name = nm ;
t h i s . courses = cr s ;
}
toString ( ) {
r e t u r n ” S t u d e n t : ” + t h i s . name+ ” t a k e s : ” + t h i s . c o u r s e s . t o S t r i n g ( ) ;
}
}
c l a s s Co u r s e {
name ; // a string
s t u d e n t s ; / / an a r r a y o f s t r i n g s
c o n s t r u c t o r ( nm , s t d s ) {
t h i s . name = nm ;
this . students = stds ;
}
toString ( ) {
r e t u r n ” Co u r s e : ” + t h i s . name+ ” i s t a ke n by : ” + t h i s . s t u d e n t s . t o S t r i n g ( ) ;
}
}
A student object consists of a name (string), and an array of taken courses (array
of strings).
A course object consists of a name (string), and an array of students taking the
course (array of string).
Nested Structure
cl a s s Student {
...
}
c l a s s Co u r s e {
...
}