SlideShare una empresa de Scribd logo
1 de 46
OBJECT ORIENTED JAVASCRIPT
Getting Started
OVERVIEW
 JavaScript is an object oriented language; it has
types and operators, core objects, and methods. Its
syntax comes from the Java and C languages, so
many structures from those languages apply to
JavaScript as well. One of the key differences is
that JavaScript does not have classes; instead, the
class functionality is accomplished by object
prototypes. The other main difference is that
functions are objects
JAVASCRIPT HAS DYNAMIC TYPES
JAVASCRIPT HAS DYNAMIC TYPES. THIS MEANS THAT THE SAME VARIABLE
CAN BE USED AS DIFFERENT TYPES:
 var x; // Now x is undefined
var x = 5; // Now x is a Number
var x = "John"; // Now x is a String
PRIMARY DATA TYPES
 The primary (primitive) data types are:
 String
 Number
 Boolean
STRING
A string is a variable which stores a series of characters like "John
Doe". Strings are written with quotes. You can use single or double
quotes:
Example
var carName = "Volvo XC60"; // Using
double quotes
var carName = 'Volvo XC60'; // Using
single quotes
Strings are concatenated using + operator
JAVASCRIPT NUMBERS
 Numbers can be written with, or without decimals:
var x1 = 34.00; // Written with decimals
var x2 = 34; // Written without decimals
 Extra large or extra small numbers can be written
with scientific (exponential) notation:
var y = 123e5; // 12300000
var z = 123e-5; // 0.00123
Example
JAVASCRIPT BOOLEANS
 Booleans can only have two values: true or false.
 var x = true;
var y = false;
 Other types are Undefined and Null, which are
slightly odd. And Arrays, which are a special kind of
object. And Dates and Regular Expressions, which
are objects that you get for free. And to be
technically accurate, functions are just a special
type of object. So the type diagram looks more like
this:
CORE OBJECTS
 JavaScript has several objects included in its core;
for example, there are objects like Math, Object,
Array, and String. The example below shows how to
use the Math object to get a random number by
using its random() method.
 you can look here for all core objects
NOTE
 Every object in JavaScript is an instance of the
Object and therefore inherits all its properties and
methods.
CUSTOM OBJECTS
The Class:
JavaScript is a prototype-based language which contains no class statement,
such as is found in C++ or Java. This is sometimes confusing for programmers
accustomed to languages with a class statement. Instead, JavaScript uses
functions as classes. Defining a class is as easy as defining a function. In the
example below we define a new class called Person.
THE OBJECT (CLASS INSTANCE)
To create a new instance of an object obj we use the
statement new obj, assigning the result (which is of type obj) to a
variable to access it later. In the example below we define a
class named Person and we create two instances
(person1 and person2).
Please also see Object.create for a new
and alternative instantiation method.
THE CONSTRUCTOR
 The constructor is called at the moment of instantiation (the moment when the
object instance is created). The constructor is a method of the class. In
JavaScript, the function serves as the constructor of the object; therefore,
there is no need to explicitly define a constructor method. Every action
declared in the class gets executed at the time of instantiation. In the example
below, the constructor of the class Person displays an alert when a Person is
instantiated.
THE PROPERTY (OBJECT ATTRIBUTE)
 Properties are variables contained in the class; every instance
of the object has those properties. Properties should be set in
the prototype property of the class (function) so that
inheritance works correctly.
 Working with properties from within the class is done by the
keyword this, which refers to the current object. Accessing
(reading or writing) a property outside of the class is done with
the syntax: InstanceName.Property; this is the same syntax
used by C++, Java, and a number of other languages. (Inside
the class the syntax this.Property is used to get or set the
property's value.)
IN THE EXAMPLE BELOW WE DEFINE
THE GENDER PROPERTY FOR THE PERSON CLASS AND WE
DEFINE IT AT INSTANTIATION
THE METHODS
 Methods follow the same logic as properties; the
difference is that they are functions and they are
defined as functions. Calling a method is similar to
accessing a property, but you add () at the end of
the method name, possibly with arguments. To
define a method, assign a function to a named
property of the class's prototype property; the name
that the function is assigned to is the name that the
method is called by on the object.
SCOPE OF VARIABLES PUBLIC/PRIVATE
 private variables are declared with the 'var' keyword
inside the object, and can only be accessed by private
functions and privileged methods.
 private functions are declared inline inside the object's
constructor (or alternatively may be defined
via var functionName=function(){...}) and may only be
called by privileged methods (including the object's
constructor).
SCOPE OF VARIABLES PUBLIC/PRIVATE
 privileged methods are declared
with this.methodName=function(){...} and may invoked by code
external to the object.
 public properties are declared with this.variableName and may
be read/written from outside the object.
 public methods are defined
by Classname.prototype.methodName = function(){...} and may
be called from outside the object.
 prototype properties are defined
by Classname.prototype.propertyName = someValue
 static properties are defined by Classname.propertyName =
someValue
PUBLIC MEMBERS
 The members of an object are all public members. Any
function can access, modify, or delete those members, or add
new members. There are two main ways of putting members
in a new object:
 In the constructor
 This technique is usually used to initialize public instance
variables. The constructor's this variable is used to add
members to the object.
PUBLIC VARIABLES
 In the prototype
 This technique is usually used to add public methods. When a
member is sought and it isn't found in the object itself, then it is
taken from the object's constructor's prototype member. The
prototype mechanism is used for inheritance. It also conserves
memory. To add a method to all objects made by a constructor,
add a function to the constructor's prototype:
http://jsfiddle.net/usmantufail/Ybd7v/6/
PRIVATE MEMBERS
 Private members are made by the constructor. Ordinary vars and
parameters of the constructor becomes the private members.
 They are attached to the object, but they are not accessible to the
outside, nor are they accessible to the object's own public methods. They
are accessible to private methods. Private methods are inner functions of
the constructor.
PRIVILEGED METHOD
 A privileged method is able to access the private
variables and methods, and is itself accessible to
the public methods and the outside. It is possible to
delete or replace a privileged method, but it is not
possible to alter it, or to force it to give up its
secrets.
 Privileged methods are assigned with this within the
constructor.
 http://jsfiddle.net/usmantufail/Ybd7v/7/
PROTOTYPE-BASED PROGRAMMING
 Prototype-based programming is a style of object-
oriented programming in which classes are not
present, and behavior reuse (known as inheritance
in class-based languages) is accomplished through
a process of decorating existing objects which
serve as prototypes. This model is also known as
class-less, prototype-oriented, or instance-based
programming.
IN THE EXAMPLE BELOW WE DEFINE AND USE THE
METHOD SAYHELLO() FOR THE PERSON CLASS.
http://jsfiddle.net/usmantufail/9ufnw/
WHAT IS NAMESPACING?
 In many programming languages, namespacing is a technique
employed to avoid collisions with other objects or variables in
the global namespace. They're also extremely useful for
helping organize blocks of functionality in your application into
easily manageable groups that can be uniquely identified.
http://jsfiddle.net/usmantufail/BDRM4/
INNER FUNCTIONS
 JavaScript function declarations are allowed inside
other functions. An important detail of nested
functions in JavaScript is that they can access
variables in their parent function's scope:
 http://jsfiddle.net/usmantufail/GFQha/2/
CLOSURES
 A closure is a function defined within another scope
that has access to all the variables within the outer
scope
 Closures are a strange concept to get to grips with,
but once this core concept is understood they’re
relatively easy to understand: a closure is created
when a developer accesses variables outside of the
immediate lexical scope.
http://jsfiddle.net/usmantufail/95PTq/1/
http://jsfiddle.net/usmantufail/95PTq/3/
MEMORY LEAKS
 An unfortunate side effect of closures is that they make it trivially easy to leak
memory in Internet Explorer. JavaScript is a garbage collected language — objects
are allocated memory upon their creation and that memory is reclaimed by the
browser when no references to an object remain. Objects provided by the host
environment are handled by that environment.
 A memory leak in IE occurs any time a circular reference is formed between a
JavaScript object and a native object. Consider the following:
The circular reference formed above creates a memory leak; IE will not
free the memory used by el and o until the browser is completely
restarted.
MODULE PATTERN
 http://jsfiddle.net/usmantufail/Emqt2/
GETTER SETTERS
 var person = {
 firstName: 'Jimmy',
 lastName: 'Smith',
 get fullName() {
 return this.firstName + ' ' + this.lastName;
 },
 set fullName (name) {
 var words = name.toString().split(' ');
 this.firstName = words[0] || '';
 this.lastName = words[1] || '';
 }
 }
 person.fullName = 'Jack Franklin';
 alert(person.firstName); // Jack
 alert(person.lastName) // Franklin
 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
 http://javascript.info/tutorial/event-delegation
 http://code.tutsplus.com/tutorials/quick-tip-javascript-event-delegation-in-4-minutes--net-8961
THREE WAYS OF DEFINING AND
INSTANTIATING AN OBJECT.
I nheritance using Closures and Prototypes
http://jsfiddle.net/usmantufail/J52uK/
INHERITANCE USING PROTOTYPING
http://jsfiddle.net/usmantufail/Ej55F/

Más contenido relacionado

La actualidad más candente

Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in phpCPD INDIA
 
09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboardsDenis Ristic
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5Sayed Ahmed
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized军 沈
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPsRavi Bhadauria
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming LanguageRaghavan Mohan
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphismmcollison
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with JavaJussi Pohjolainen
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript ProgrammingSehwan Noh
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 

La actualidad más candente (20)

Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5
 
Classes&objects
Classes&objectsClasses&objects
Classes&objects
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
 
Js: master prototypes
Js: master prototypesJs: master prototypes
Js: master prototypes
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 
Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Computer Programming 2
Computer Programming 2 Computer Programming 2
Computer Programming 2
 
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Prototype
PrototypePrototype
Prototype
 
PHP Classes and OOPS Concept
PHP Classes and OOPS ConceptPHP Classes and OOPS Concept
PHP Classes and OOPS Concept
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
 

Similar a JavaScript OOPS Implimentation

FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...AnkurSingh340457
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)Khaled Anaqwa
 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptxrani marri
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2Rakesh Madugula
 
Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02Sopheak Sem
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
Java interview questions and answers
Java interview questions and answersJava interview questions and answers
Java interview questions and answersKrishnaov
 
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...Ayes Chinmay
 
Nitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptxNitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptxNitishChaulagai
 
DeclaringConstructir.pptx
DeclaringConstructir.pptxDeclaringConstructir.pptx
DeclaringConstructir.pptxSinbadMagi
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorialGhulam Abbas Khan
 
OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptxmrxyz19
 
oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfArpitaJana28
 

Similar a JavaScript OOPS Implimentation (20)

FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptx
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
 
Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02
 
Oops
OopsOops
Oops
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
My c++
My c++My c++
My c++
 
Java interview questions and answers
Java interview questions and answersJava interview questions and answers
Java interview questions and answers
 
Oops
OopsOops
Oops
 
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
 
Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
 
Nitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptxNitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptx
 
Java notes
Java notesJava notes
Java notes
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Only oop
Only oopOnly oop
Only oop
 
DeclaringConstructir.pptx
DeclaringConstructir.pptxDeclaringConstructir.pptx
DeclaringConstructir.pptx
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorial
 
OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptx
 
oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdf
 

Último

DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptxJonalynLegaspi2
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 

Último (20)

DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptx
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 

JavaScript OOPS Implimentation

  • 2. OVERVIEW  JavaScript is an object oriented language; it has types and operators, core objects, and methods. Its syntax comes from the Java and C languages, so many structures from those languages apply to JavaScript as well. One of the key differences is that JavaScript does not have classes; instead, the class functionality is accomplished by object prototypes. The other main difference is that functions are objects
  • 3. JAVASCRIPT HAS DYNAMIC TYPES JAVASCRIPT HAS DYNAMIC TYPES. THIS MEANS THAT THE SAME VARIABLE CAN BE USED AS DIFFERENT TYPES:  var x; // Now x is undefined var x = 5; // Now x is a Number var x = "John"; // Now x is a String
  • 4. PRIMARY DATA TYPES  The primary (primitive) data types are:  String  Number  Boolean
  • 5. STRING A string is a variable which stores a series of characters like "John Doe". Strings are written with quotes. You can use single or double quotes: Example var carName = "Volvo XC60"; // Using double quotes var carName = 'Volvo XC60'; // Using single quotes Strings are concatenated using + operator
  • 6. JAVASCRIPT NUMBERS  Numbers can be written with, or without decimals: var x1 = 34.00; // Written with decimals var x2 = 34; // Written without decimals  Extra large or extra small numbers can be written with scientific (exponential) notation: var y = 123e5; // 12300000 var z = 123e-5; // 0.00123 Example
  • 7. JAVASCRIPT BOOLEANS  Booleans can only have two values: true or false.  var x = true; var y = false;
  • 8.  Other types are Undefined and Null, which are slightly odd. And Arrays, which are a special kind of object. And Dates and Regular Expressions, which are objects that you get for free. And to be technically accurate, functions are just a special type of object. So the type diagram looks more like this:
  • 9. CORE OBJECTS  JavaScript has several objects included in its core; for example, there are objects like Math, Object, Array, and String. The example below shows how to use the Math object to get a random number by using its random() method.  you can look here for all core objects
  • 10. NOTE  Every object in JavaScript is an instance of the Object and therefore inherits all its properties and methods.
  • 11. CUSTOM OBJECTS The Class: JavaScript is a prototype-based language which contains no class statement, such as is found in C++ or Java. This is sometimes confusing for programmers accustomed to languages with a class statement. Instead, JavaScript uses functions as classes. Defining a class is as easy as defining a function. In the example below we define a new class called Person.
  • 12. THE OBJECT (CLASS INSTANCE) To create a new instance of an object obj we use the statement new obj, assigning the result (which is of type obj) to a variable to access it later. In the example below we define a class named Person and we create two instances (person1 and person2). Please also see Object.create for a new and alternative instantiation method.
  • 13. THE CONSTRUCTOR  The constructor is called at the moment of instantiation (the moment when the object instance is created). The constructor is a method of the class. In JavaScript, the function serves as the constructor of the object; therefore, there is no need to explicitly define a constructor method. Every action declared in the class gets executed at the time of instantiation. In the example below, the constructor of the class Person displays an alert when a Person is instantiated.
  • 14. THE PROPERTY (OBJECT ATTRIBUTE)  Properties are variables contained in the class; every instance of the object has those properties. Properties should be set in the prototype property of the class (function) so that inheritance works correctly.  Working with properties from within the class is done by the keyword this, which refers to the current object. Accessing (reading or writing) a property outside of the class is done with the syntax: InstanceName.Property; this is the same syntax used by C++, Java, and a number of other languages. (Inside the class the syntax this.Property is used to get or set the property's value.)
  • 15. IN THE EXAMPLE BELOW WE DEFINE THE GENDER PROPERTY FOR THE PERSON CLASS AND WE DEFINE IT AT INSTANTIATION
  • 16. THE METHODS  Methods follow the same logic as properties; the difference is that they are functions and they are defined as functions. Calling a method is similar to accessing a property, but you add () at the end of the method name, possibly with arguments. To define a method, assign a function to a named property of the class's prototype property; the name that the function is assigned to is the name that the method is called by on the object.
  • 17. SCOPE OF VARIABLES PUBLIC/PRIVATE  private variables are declared with the 'var' keyword inside the object, and can only be accessed by private functions and privileged methods.  private functions are declared inline inside the object's constructor (or alternatively may be defined via var functionName=function(){...}) and may only be called by privileged methods (including the object's constructor).
  • 18. SCOPE OF VARIABLES PUBLIC/PRIVATE  privileged methods are declared with this.methodName=function(){...} and may invoked by code external to the object.  public properties are declared with this.variableName and may be read/written from outside the object.  public methods are defined by Classname.prototype.methodName = function(){...} and may be called from outside the object.  prototype properties are defined by Classname.prototype.propertyName = someValue  static properties are defined by Classname.propertyName = someValue
  • 19. PUBLIC MEMBERS  The members of an object are all public members. Any function can access, modify, or delete those members, or add new members. There are two main ways of putting members in a new object:  In the constructor  This technique is usually used to initialize public instance variables. The constructor's this variable is used to add members to the object.
  • 20. PUBLIC VARIABLES  In the prototype  This technique is usually used to add public methods. When a member is sought and it isn't found in the object itself, then it is taken from the object's constructor's prototype member. The prototype mechanism is used for inheritance. It also conserves memory. To add a method to all objects made by a constructor, add a function to the constructor's prototype: http://jsfiddle.net/usmantufail/Ybd7v/6/
  • 21. PRIVATE MEMBERS  Private members are made by the constructor. Ordinary vars and parameters of the constructor becomes the private members.  They are attached to the object, but they are not accessible to the outside, nor are they accessible to the object's own public methods. They are accessible to private methods. Private methods are inner functions of the constructor.
  • 22. PRIVILEGED METHOD  A privileged method is able to access the private variables and methods, and is itself accessible to the public methods and the outside. It is possible to delete or replace a privileged method, but it is not possible to alter it, or to force it to give up its secrets.  Privileged methods are assigned with this within the constructor.  http://jsfiddle.net/usmantufail/Ybd7v/7/
  • 23. PROTOTYPE-BASED PROGRAMMING  Prototype-based programming is a style of object- oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based languages) is accomplished through a process of decorating existing objects which serve as prototypes. This model is also known as class-less, prototype-oriented, or instance-based programming.
  • 24. IN THE EXAMPLE BELOW WE DEFINE AND USE THE METHOD SAYHELLO() FOR THE PERSON CLASS. http://jsfiddle.net/usmantufail/9ufnw/
  • 25. WHAT IS NAMESPACING?  In many programming languages, namespacing is a technique employed to avoid collisions with other objects or variables in the global namespace. They're also extremely useful for helping organize blocks of functionality in your application into easily manageable groups that can be uniquely identified. http://jsfiddle.net/usmantufail/BDRM4/
  • 26. INNER FUNCTIONS  JavaScript function declarations are allowed inside other functions. An important detail of nested functions in JavaScript is that they can access variables in their parent function's scope:  http://jsfiddle.net/usmantufail/GFQha/2/
  • 27. CLOSURES  A closure is a function defined within another scope that has access to all the variables within the outer scope  Closures are a strange concept to get to grips with, but once this core concept is understood they’re relatively easy to understand: a closure is created when a developer accesses variables outside of the immediate lexical scope. http://jsfiddle.net/usmantufail/95PTq/1/ http://jsfiddle.net/usmantufail/95PTq/3/
  • 28. MEMORY LEAKS  An unfortunate side effect of closures is that they make it trivially easy to leak memory in Internet Explorer. JavaScript is a garbage collected language — objects are allocated memory upon their creation and that memory is reclaimed by the browser when no references to an object remain. Objects provided by the host environment are handled by that environment.  A memory leak in IE occurs any time a circular reference is formed between a JavaScript object and a native object. Consider the following: The circular reference formed above creates a memory leak; IE will not free the memory used by el and o until the browser is completely restarted.
  • 30. GETTER SETTERS  var person = {  firstName: 'Jimmy',  lastName: 'Smith',  get fullName() {  return this.firstName + ' ' + this.lastName;  },  set fullName (name) {  var words = name.toString().split(' ');  this.firstName = words[0] || '';  this.lastName = words[1] || '';  }  }  person.fullName = 'Jack Franklin';  alert(person.firstName); // Jack  alert(person.lastName) // Franklin  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty  http://javascript.info/tutorial/event-delegation  http://code.tutsplus.com/tutorials/quick-tip-javascript-event-delegation-in-4-minutes--net-8961
  • 31. THREE WAYS OF DEFINING AND INSTANTIATING AN OBJECT.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44. I nheritance using Closures and Prototypes