SlideShare a Scribd company logo
04 October 2009 Barcamp Phnom Penh 2009
About Us Lim Borey Software Developer Chhorn Chamnap Software Developer
Agenda Debugging Tool Data types Object Constructor Inheritance & Prototype Summary Q & A
Debugging Tool Firebug – Firefox extension
JavaScript !== Java C-like syntax   Classes  
Data types A. Primitive: number –  1 ,  3 ,  1001 ,  11.12 ,  2e+3 string –  "a" ,  "stoyan" ,  "0" boolean –  true  |  false null undefined B. Objects everything else…
Objects hash tables  key: value
A simple object var  obj = {}; obj.name =  'my object' ; obj.shiny =  true ;
A simple object var  obj =  { shiny:  true , isShiny:  function () { return   this .shiny; } } ; obj.isShiny();  // true
Methods When a property is a function we can call it a method  var object = { method: function(){ alert( "here’s method" ); } }
Object literal notation Key-value pairs Comma-delimited Wrapped in curly braces {a: 1, b: "test"}
Arrays Arrays are objects too Auto-increment properties Some useful methods
Arrays >>>  var  a = [1,3,2]; >>> a[0] 1 >>>  typeof  a "object" >>>  var  b = []; >>> b.push("value"); >>> console.log(b);  //   "value"
JSON Object literals + array literals JavaScript Object Notation {"num":  1 , "str":  "abc" ,  "arr": [ 1 , 2 , 3 ]}
Constructor Is called at the moment of instantiation In JavaScript, the function serves as the constructor of the object var  Person  = function () {  alert('Person instantiated');  }  var  person1 = new Person();  var  person2 = new Person();
constructor  property >>>  function  Person(){}; >>>  var  mony =  new  Person(); >>> mony. constructor  === Person true
constructor  property >>>  var  obj = {}; >>> obj. constructor  === Object true >>> [1,2]. constructor  === Array true
Built-in constructors Object Array Function RegExp Number String Boolean Date Error, SyntaxError, ReferenceError…
Inheritance & Prototype
Inheritance A method to create a class as a specialized version of one or more classes JavaScript only supports single class inheritance
// define the Person Class  function Person() {} Person.prototype.walk = function(){}; Person.prototype.sayHello = function(){  alert ('hello');  };  // define the Student class inherit Person  function Student() {}  Student.prototype = new Person();  Student.prototype.constructor = Student;  // override the sayHello method  Student.prototype.sayHello = function(){ alert('hi, I am a student');  }; var student1 = new Student();  student1.sayHello();
Prototype … …  is a property of the function objects >>>  var  boo =  function (){}; >>>  typeof  boo. prototype "object"
Use of the prototype The prototype is used when a function is called as a constructor
Prototype usage var  Person =  function (name) { this .name = name; }; Person. prototype .say =  function (){ return this .name; };
Prototype usage >>>  var  dude =  new  Person( 'dude' ); >>> dude.name; "dude" >>> dude.say(); "dude"
Prototype usage say()  is a property of the  prototype  object but it behaves as if it's a property of the dude object
Own properties vs. prototype’s >>> dude. hasOwnProperty ( 'name' ); true >>> dude. hasOwnProperty ( 'say' ); false
isPrototypeOf() >>> Person. prototype . isPrototypeOf (dude); true >>>  Object . prototype . isPrototypeOf (dude); true
__proto__ The objects have a secret link to the prototype of the constructor that created them __proto__ is not directly exposed in all browsers
__proto__ >>> dude.__proto__. hasOwnProperty ( 'say' ) true >>> dude.__proto__.__proto__. hasOwnProperty ( 'toString' ) true
Prototype chain
It’s a live chain >>>  typeof  dude.numlegs "undefined" >>> Person. prototype .numlegs =  2 ; >>> dude.numlegs 2
It’s a live chain >>>  typeof  dude.numlegs "undefined" >>> Person. prototype .numlegs =  2 ; >>> dude.numlegs 2
Summary
Objects Everything is an object (except a few primitive types) Objects are hashes Arrays are objects  Class – no such thing in JavaScript A function meant to be called with  new Returns an object Inheritance Support singular inheritance Prototype Property of the function objects It’s an object  Useful with Constructor functions
References Object-Oriented JavaScript, Stoyan Stefanov Pro JavaScript™ Techniques, John Resig https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript
 
 

More Related Content

PPT
Object Oriented JavaScript
PPT
Beginning Object-Oriented JavaScript
PPTX
Understanding Object Oriented Javascript - Coffee@DBG June
PPT
Advanced Javascript
PPT
Advanced JavaScript
PDF
Object Oriented Programming in JavaScript
PPTX
Advanced JavaScript
PPT
Advanced JavaScript
Object Oriented JavaScript
Beginning Object-Oriented JavaScript
Understanding Object Oriented Javascript - Coffee@DBG June
Advanced Javascript
Advanced JavaScript
Object Oriented Programming in JavaScript
Advanced JavaScript
Advanced JavaScript

What's hot (20)

PDF
Core concepts-javascript
PDF
Object Oriented JavaScript
PDF
Java Script Best Practices
PDF
Advanced javascript
PPT
A Deeper look into Javascript Basics
PDF
JavaScript Basics and Best Practices - CC FE & UX
PPTX
Javascript basics for automation testing
PPT
JavaScript Basics
PDF
Powerful JavaScript Tips and Best Practices
KEY
JavaScript Growing Up
ODP
Javascript
PDF
Java script object model
PDF
JavaScript Inheritance
KEY
Javascript tid-bits
PDF
JavaScript Patterns
PDF
JavaScript - From Birth To Closure
PDF
Basics of JavaScript
PPT
Javascript Object Oriented Programming
PDF
JavaScript for PHP developers
PPT
Core concepts-javascript
Object Oriented JavaScript
Java Script Best Practices
Advanced javascript
A Deeper look into Javascript Basics
JavaScript Basics and Best Practices - CC FE & UX
Javascript basics for automation testing
JavaScript Basics
Powerful JavaScript Tips and Best Practices
JavaScript Growing Up
Javascript
Java script object model
JavaScript Inheritance
Javascript tid-bits
JavaScript Patterns
JavaScript - From Birth To Closure
Basics of JavaScript
Javascript Object Oriented Programming
JavaScript for PHP developers
Ad

Similar to JavaScript In Object Oriented Way (20)

PPT
Javascript Primer
PPTX
PPTX
jQuery Data Manipulate API - A source code dissecting journey
PPT
JavaScript Needn't Hurt!
PPTX
Object Oriented JavaScript
PPT
Javascript Templating
PPT
Javascript Ks
PPT
Presentation
PPT
Javascript quiz. Questions to ask when recruiting developers.
PPT
JSConf: All You Can Leet
PPT
Javascript Design Patterns
PPT
PDF
Javascript Design Patterns
PPT
Web Optimization Summit: Coding for Performance
PPTX
OO in JavaScript
PPT
Java: Objects and Object References
PPTX
Object Oriented Programming In JavaScript
PPTX
Javascript Objects Deep Dive
PPTX
JavaScript Literacy
Javascript Primer
jQuery Data Manipulate API - A source code dissecting journey
JavaScript Needn't Hurt!
Object Oriented JavaScript
Javascript Templating
Javascript Ks
Presentation
Javascript quiz. Questions to ask when recruiting developers.
JSConf: All You Can Leet
Javascript Design Patterns
Javascript Design Patterns
Web Optimization Summit: Coding for Performance
OO in JavaScript
Java: Objects and Object References
Object Oriented Programming In JavaScript
Javascript Objects Deep Dive
JavaScript Literacy
Ad

Recently uploaded (20)

PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
01-Introduction-to-Information-Management.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Pharma ospi slides which help in ospi learning
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
01-Introduction-to-Information-Management.pdf
Anesthesia in Laparoscopic Surgery in India
VCE English Exam - Section C Student Revision Booklet
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
2.FourierTransform-ShortQuestionswithAnswers.pdf
Pre independence Education in Inndia.pdf
Renaissance Architecture: A Journey from Faith to Humanism
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Pharma ospi slides which help in ospi learning
FourierSeries-QuestionsWithAnswers(Part-A).pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Microbial disease of the cardiovascular and lymphatic systems
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
STATICS OF THE RIGID BODIES Hibbelers.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?

JavaScript In Object Oriented Way

  • 1. 04 October 2009 Barcamp Phnom Penh 2009
  • 2. About Us Lim Borey Software Developer Chhorn Chamnap Software Developer
  • 3. Agenda Debugging Tool Data types Object Constructor Inheritance & Prototype Summary Q & A
  • 4. Debugging Tool Firebug – Firefox extension
  • 5. JavaScript !== Java C-like syntax  Classes 
  • 6. Data types A. Primitive: number – 1 , 3 , 1001 , 11.12 , 2e+3 string – "a" , "stoyan" , "0" boolean – true | false null undefined B. Objects everything else…
  • 7. Objects hash tables key: value
  • 8. A simple object var obj = {}; obj.name = 'my object' ; obj.shiny = true ;
  • 9. A simple object var obj = { shiny: true , isShiny: function () { return this .shiny; } } ; obj.isShiny(); // true
  • 10. Methods When a property is a function we can call it a method var object = { method: function(){ alert( "here’s method" ); } }
  • 11. Object literal notation Key-value pairs Comma-delimited Wrapped in curly braces {a: 1, b: "test"}
  • 12. Arrays Arrays are objects too Auto-increment properties Some useful methods
  • 13. Arrays >>> var a = [1,3,2]; >>> a[0] 1 >>> typeof a "object" >>> var b = []; >>> b.push("value"); >>> console.log(b); // "value"
  • 14. JSON Object literals + array literals JavaScript Object Notation {"num": 1 , "str": "abc" , "arr": [ 1 , 2 , 3 ]}
  • 15. Constructor Is called at the moment of instantiation In JavaScript, the function serves as the constructor of the object var Person = function () { alert('Person instantiated'); } var person1 = new Person(); var person2 = new Person();
  • 16. constructor property >>> function Person(){}; >>> var mony = new Person(); >>> mony. constructor === Person true
  • 17. constructor property >>> var obj = {}; >>> obj. constructor === Object true >>> [1,2]. constructor === Array true
  • 18. Built-in constructors Object Array Function RegExp Number String Boolean Date Error, SyntaxError, ReferenceError…
  • 20. Inheritance A method to create a class as a specialized version of one or more classes JavaScript only supports single class inheritance
  • 21. // define the Person Class function Person() {} Person.prototype.walk = function(){}; Person.prototype.sayHello = function(){ alert ('hello'); }; // define the Student class inherit Person function Student() {} Student.prototype = new Person(); Student.prototype.constructor = Student; // override the sayHello method Student.prototype.sayHello = function(){ alert('hi, I am a student'); }; var student1 = new Student(); student1.sayHello();
  • 22. Prototype … … is a property of the function objects >>> var boo = function (){}; >>> typeof boo. prototype "object"
  • 23. Use of the prototype The prototype is used when a function is called as a constructor
  • 24. Prototype usage var Person = function (name) { this .name = name; }; Person. prototype .say = function (){ return this .name; };
  • 25. Prototype usage >>> var dude = new Person( 'dude' ); >>> dude.name; "dude" >>> dude.say(); "dude"
  • 26. Prototype usage say() is a property of the prototype object but it behaves as if it's a property of the dude object
  • 27. Own properties vs. prototype’s >>> dude. hasOwnProperty ( 'name' ); true >>> dude. hasOwnProperty ( 'say' ); false
  • 28. isPrototypeOf() >>> Person. prototype . isPrototypeOf (dude); true >>> Object . prototype . isPrototypeOf (dude); true
  • 29. __proto__ The objects have a secret link to the prototype of the constructor that created them __proto__ is not directly exposed in all browsers
  • 30. __proto__ >>> dude.__proto__. hasOwnProperty ( 'say' ) true >>> dude.__proto__.__proto__. hasOwnProperty ( 'toString' ) true
  • 32. It’s a live chain >>> typeof dude.numlegs "undefined" >>> Person. prototype .numlegs = 2 ; >>> dude.numlegs 2
  • 33. It’s a live chain >>> typeof dude.numlegs "undefined" >>> Person. prototype .numlegs = 2 ; >>> dude.numlegs 2
  • 35. Objects Everything is an object (except a few primitive types) Objects are hashes Arrays are objects Class – no such thing in JavaScript A function meant to be called with new Returns an object Inheritance Support singular inheritance Prototype Property of the function objects It’s an object Useful with Constructor functions
  • 36. References Object-Oriented JavaScript, Stoyan Stefanov Pro JavaScript™ Techniques, John Resig https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript
  • 37.  
  • 38.