38
04 October 2009 Barcamp Phnom Penh 2009

JavaScript In Object Oriented Way

Embed Size (px)

DESCRIPTION

presentation at barcamp phnom penh 2009

Citation preview

Page 1: JavaScript In Object Oriented Way

04 October 2009Barcamp Phnom Penh 2009

Page 2: JavaScript In Object Oriented Way

About UsAbout Us

Lim Borey

Software Developer

Chhorn Chamnap

Software Developer

Page 3: JavaScript In Object Oriented Way

Agenda Debugging Tool

Data types

Object

Constructor

Inheritance & Prototype

Summary

Q & A

Page 4: JavaScript In Object Oriented Way

Debugging Tool

Firebug – Firefox extension

Page 5: JavaScript In Object Oriented Way

JavaScript !== Java C-like syntax Classes

Page 6: JavaScript In Object Oriented Way

Data types

A. Primitive:number – 1, 3, 1001, 11.12, 2e+3string – "a", "stoyan", "0"boolean – true | falsenullundefined

B. Objectseverything else…

Page 7: JavaScript In Object Oriented Way

Objects hash tables key: value

Page 8: JavaScript In Object Oriented Way

A simple objectvar obj = {};

obj.name = 'my object';

obj.shiny = true;

Page 9: JavaScript In Object Oriented Way

A simple object

var obj = {

shiny: true,

isShiny: function() {

return this.shiny;

}

};

obj.isShiny(); // true

Page 10: JavaScript In Object Oriented Way

Methods When a property is a functionwe can call it a method

var object = {

method: function(){

alert("here’s method");

}

}

Page 11: JavaScript In Object Oriented Way

Object literal notation

Key-value pairs Comma-delimited Wrapped in curly braces

{a: 1, b: "test"}

Page 12: JavaScript In Object Oriented Way

Arrays Arrays are objects too Auto-increment properties Some useful methods

Page 13: JavaScript In Object Oriented Way

Arrays>>> var a = [1,3,2];

>>> a[0]

1

>>> typeof a

"object"

>>> var b = [];>>> b.push("value");

>>> console.log(b);

// "value"

Page 14: JavaScript In Object Oriented Way

JSON Object literals + array literals JavaScript Object Notation

{"num": 1, "str": "abc", "arr": [1,2,3]}

Page 15: JavaScript In Object Oriented Way

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();

Page 16: JavaScript In Object Oriented Way

constructor property>>> function Person(){};

>>> var mony = new Person();

>>> mony.constructor === Person

true

Page 17: JavaScript In Object Oriented Way

constructor property>>> var obj = {};

>>> obj.constructor === Object

true

>>> [1,2].constructor === Array

true

Page 18: JavaScript In Object Oriented Way

Built-in constructors

Object Array Function RegExp Number String Boolean Date Error, SyntaxError, ReferenceError…

Page 19: JavaScript In Object Oriented Way

Inheritance & Prototype

Page 20: JavaScript In Object Oriented Way

Inheritance A method to create a class as a specialized version of one or more classes

JavaScript only supports single class inheritance

Page 21: JavaScript In Object Oriented Way

// 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();

Page 22: JavaScript In Object Oriented Way

Prototype…… is a property of the function objects

>>> var boo = function(){};

>>> typeof boo.prototype

"object"

Page 23: JavaScript In Object Oriented Way

Use of the prototype The prototype is used when a function is called as a constructor

Page 24: JavaScript In Object Oriented Way

Prototype usage

var Person = function(name) {

this.name = name;

};

Person.prototype.say = function(){

return this.name;

};

Page 25: JavaScript In Object Oriented Way

Prototype usage>>> var dude = new Person('dude');

>>> dude.name;

"dude"

>>> dude.say();

"dude"

Page 26: JavaScript In Object Oriented Way

Prototype usage say() is a property of the prototype object

but it behaves as if it's a property of the dude object

Page 27: JavaScript In Object Oriented Way

Own properties vs. prototype’s

>>> dude.hasOwnProperty('name');

true

>>> dude.hasOwnProperty('say');

false

Page 28: JavaScript In Object Oriented Way

isPrototypeOf()

>>> Person.prototype.isPrototypeOf(dude);

true

>>> Object.prototype.isPrototypeOf(dude);

true

Page 29: JavaScript In Object Oriented Way

__proto__ The objects have a secret link to the prototype of the constructor that created them

__proto__ is not directly exposed in all browsers

Page 30: JavaScript In Object Oriented Way

__proto__

>>> dude.__proto__.hasOwnProperty('say')true

>>> dude.__proto__.__proto__.hasOwnProperty('toString')

true

Page 31: JavaScript In Object Oriented Way

Prototype chain

Page 32: JavaScript In Object Oriented Way

It’s a live chain

>>> typeof dude.numlegs

"undefined"

>>> Person.prototype.numlegs = 2;

>>> dude.numlegs

2

Page 33: JavaScript In Object Oriented Way

It’s a live chain

>>> typeof dude.numlegs

"undefined"

>>> Person.prototype.numlegs = 2;

>>> dude.numlegs

2

Page 34: JavaScript In Object Oriented Way

Summary

Page 35: JavaScript In Object Oriented Way

ObjectsEverything is an object (except a few primitive types)

Objects are hashesArrays are objects

Class – no such thing in JavaScriptA function meant to be called with newReturns an object

InheritanceSupport singular inheritance

PrototypeProperty of the function objectsIt’s an object Useful with Constructor functions

Page 36: JavaScript In Object Oriented Way

References

Object-Oriented JavaScript, Stoyan Stefanov

Pro JavaScript™ Techniques, John Resig

https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript

Page 37: JavaScript In Object Oriented Way
Page 38: JavaScript In Object Oriented Way