This document discusses JavaScript objects and object-oriented programming (OOP) in JavaScript. It introduces the author and defines JavaScript as an object-oriented language, noting that everything in JavaScript besides five primitive types (Number, String, Boolean, undefined, null) is an object. It describes creating user-defined objects using the new Object() constructor, functions as objects, object methods, and object literals - a lightweight syntax for creating objects. Examples are provided for each concept to illustrate OOP techniques in JavaScript.
Who I’m
● Myname is Hazem Hagrass
● Lead Software Engineer @BADR, joined BADR @2010
● Contacts:
○ website
○ hazem.hagrass@badrit.com
○ Linkedin
4.
Introduction
● JavaScript isobject-oriented language.
● Not really object-oriented language.
● There are multiple ways to do OOP in JS.
● There are 5 primitives only in JS: Number, String,
Boolean, undefined, null.
● Everything else is an Object.
5.
User Defined Objects
●All user-defined objects and built-in objects are
descendants of an object called Object.
var employee = new Object();
employee.name = “Hazem Hagrass”;
var books = new Array("C++", "Perl", "Java");
var day = new Date("August 15, 1947");
6.
User Defined Objects
●new Object() produces an empty container of
name/value pairs.
● Its nature like hash but no hash codes or rehash codes
are available.
7.
Objects with aUser-Defined Function
● Functions are first-class citizens in JS, they can
contain number of methods, so functions are objects
that can contain variables and methods.
● this keyword is used to refer to the object that has
been passed to a function.
function employee(name, departement){
this.name = name;
this.departement = departement;
}
var emp = new employee(“Hazem Hagrass”, “IT”);
Object Literal
● Objectliterals encapsulate data, enclosing it in a tidy
package.
● This minimizes the use of global variables which can
cause problems when combining code.
var Coor = {
x: 0,
y: 0
}
console.log(coor.x);
Why Object Literal?
●Object literals enable us to write code that supports
lots of features yet still provide a relatively
straightforward process for implementers of our code.
● No need to invoke constructors directly or maintain
the correct order of arguments passed to functions.
● Object literals are also useful for unobtrusive event
handling; they can hold the data that would otherwise
be passed in function calls from HTML event handler
attributes.