3. OBJECT ORIENTED PROGRAMMING
Object Oriented Programming – A programming paradigm where we use Objects to model real
world things.
Object – Collection of related data and/or functionality. (Encapsulation)
Variables and Functions — are called properties and methods when they are inside objects.
4. OBJECTS IN JAVASCRIPT
Ways of creating an Object in JavaScript
1. var myObj1 = { };
2. var myObj2 = new Object();
3. var myObj3 = function () { var obj = {}; return obj;};
4. function MyObj4() { this.myVar = ‘’; };
The constructor function is JavaScript's version of a class. (No: 4)
A constructor function name usually starts with a capital letter — this convention is used to make
constructor functions easier to recognize in code.
5. OBJECTS IN JAVASCRIPT (CONTD..)
Object Format
var objectName = {
member1Name: member1Value,
member2Name: member2Value,
member3Name: member3Value
}
Each name/value pair must be
separated by a comma
The name and value in each case are
separated by a colon
Sample Object
var person = {
name : {
first: 'Bob',
last: 'Smith‘
},
age: 32,
gender: 'male',
greeting: function() {
alert('Hi! I am ' + this.name[0]);
}
};
6. ACCESS OBJECT PROPERTIES
Dot Notation
person.age, person.gender
Person.name.first, person.name.last
Bracket Notation
Using Property:
person[‘age’], person[‘gender’]
person[‘name’][‘first’], person[‘name’][‘last’]
Using Index:
person[0], person[1]
Person[0][‘first’], Person[0].first
Difference
Dot notation can only accept a literal member name, not a variable value pointing to a name
7. SET OBJECT PROPERTIES
person.age = 45
person['name']['last'] = 'Cratchit‘
We can also create new members.
person['eyes'] = 'hazel'
person.farewell = function() { alert("Bye everybody!") }
8. ‘THIS’ KEYWORD
The this keyword refers to the current object
the code is being written inside.
Eg. Two different person object instances
may have different names, but will want to
use their own name when saying their
greeting.
Person1.greeting() -> ‘Hi I’m Chris’
Person2.greeting() -> ‘Hi I’m Brian’
9. PROTOTYPE
We have to understand Prototypes to understand Inheritance.
Each object has a prototype object, which acts as a template object that it inherits methods and properties
from.
Prototypes are the mechanism by which JavaScript objects inherit features from one another.
Prototype Chain : An object's prototype object may also have a prototype object, which it inherits methods
and properties from, and so on
In classic OOP, classes are defined, then when object instances are created all the properties and methods
defined on the class are copied over to the instance.
In JavaScript, they are not copied over — instead, a link is made between the object instance and its
constructor (a link in the prototype chain), and the properties and methods are found in the constructor by
walking up the chain.
10. PROTOTYPAL INHERITANCE
How do we create an Object in JavaScript that inherits from another Object? (other than built-in
Objects)
Only Constructors have prototype property. Object instances do not have it.
Steps to Inherit from another Object
Create the Main constructor
Create the Child constructor
Set Prototype
Set Constructor Reference