lecture 14 object oriented javascript · every object in fact is a hash table internally (key:...

35
SE-805 Web 2.0 Programming (supported by Google) http://my.ss.sysu.edu.cn/courses/web2.0/ School of Software, Sun Yat-sen University Lecture 14 Object Oriented JavaScript

Upload: others

Post on 09-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

SE-805 Web 2.0 Programming (supported by Google)http://my.ss.sysu.edu.cn/courses/web2.0/

School of Software, Sun Yat-sen University

Lecture 14 Object Oriented JavaScript

Page 2: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

2 / 35July 1, 2010

Outline

Learning Tools Object and Functions Constructors and Prototype Inheritance Polymorphism

Page 3: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

3 / 35July 1, 2010

Learning Tool – Firebug

Page 4: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

4 / 35July 1, 2010

Learning Tool – JavaScript Shell

Using bookmarklet version of shell: commands will run in opener's context• https://www.squarefree.com/bookmarklets/webdevel.html

Page 5: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

5 / 35July 1, 2010

Outline

Learning Tools Object and Functions Constructors and Prototype Inheritance Polymorphism

Page 6: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

6 / 35July 1, 2010

JavaScript != Java

C-like syntax Classes Data type:

• Primitive:• Number – 1, 3, 1001, 11.12, 2e+3• String – "a", "stoyan", "0"• Boolean – true | false• null• undefined

• Objects: everything else …

Page 7: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

7 / 35July 1, 2010

Objects

Every object in fact is a hash table internally (key: value) When a property is a function we can call it a methodvar obj = {};

obj.name = 'my object';

obj.shiny = true;

var obj = {

shiny: true,

isShiny: function() {

return this.shiny;

}

};

obj.isShiny(); // true

Page 8: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

8 / 35July 1, 2010

Object Literal Notation

Key-value pairs Comma-delimited Wrapped in curly braces

{a: 1, b: "test"}

Page 9: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

9 / 35July 1, 2010

Arrays

Arrays are objects too Auto-increment properties Some useful methods

Array literal notation

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

>>> a[0]

1

>>> typeof a

"object"

var array = [

"Square",

"brackets",

"wrap",

"the",

"comma-delimited",

"elements"

];

Page 10: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

10 / 35July 1, 2010

JavaScript Object Notation (JSON)

Object literals + array literals

Serialization of Objects, useful in persisting and transporting Objects

A JSON literal string can be embodied via the eval() function

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

var jsonStr = '{"num": 1, "str": "abc", "arr": [1,2,3]}‘;

obj = eval(jsonStr);

Page 11: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

11 / 35July 1, 2010

Functions

Functions are objects• They have properties• They have methods• Can be copied, deleted, augmented...• Special feature: invokable

function say(what) {

return what;

}

var say = function(what) {

return what;

};

var say = function say(what) {

return what;

};

Page 12: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

12 / 35July 1, 2010

Functions are Objects

>>> say.length

1

>>> say.name

"boo"

>>> var tell = say;

>>> tell("doodles")

"doodles"

>>> tell.call(null, "moo!");

"moo!"

Page 13: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

13 / 35July 1, 2010

Return Values

All functions always return a value If a function doesn’t return a value explicitly, it returns

undefined Functions can return objects, including other functions

Page 14: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

14 / 35July 1, 2010

Outline

Learning Tools Object and Functions Constructors and Prototype Inheritance Polymorphism

Page 15: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

15 / 35July 1, 2010

Construction Functions

When invoked with new, functions return an object known as this

You can modify this before it’s returned Naming convention: MyConstructor,myFunctionvar Person = function(name) {

this.name = name;

this.getName = function() {

return this.name;

};

};

var me = new Person("Stoyan");

me.getName(); // "Stoyan"

Page 16: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

16 / 35July 1, 2010

Constructor Property

>>> function Person(){};

>>> var jo = new Person();

>>> jo.constructor === Person

true

>>> var o = {};

>>> o.constructor === Object

true

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

true

Page 17: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

17 / 35July 1, 2010

Built-in Constructors

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

Page 18: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

18 / 35July 1, 2010

Conventions

Use this Not that

var o = {}; var o = new Object();

var a = []; var a = new Array();

var re = /[a-z]/gmi; var re = new RegExp('[a-z]', 'gmi');

var fn = function(a, b){return a + b;

}

var fn = new Function('a, b','return a+b');

Page 19: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

19 / 35July 1, 2010

Prototype prototype is a special property of the function objects prototype is NOT the JavaScript toolkit we used

Augmenting the prototype

Overwriting the prototype

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

>>> typeof boo.prototype

"object"

>>> boo.prototype.a = 1;

>>> boo.prototype.sayAh = function(){};

>>> boo.prototype ={a: 1, b: 2};

Page 20: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

20 / 35July 1, 2010

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

constructorvar Person = function(name) {

this.name = name;

};

Person.prototype.say = function(){

return this.name;

};

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

>>> dude.name;

"dude"

>>> dude.say();

"dude"

say() is a property of the prototype object but it behaves as if it's a property of the dude object

Page 21: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

21 / 35July 1, 2010

isPrototypeOf()

Own Properties vs. Prototype’s>>> dude.hasOwnProperty('name');

true

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

false

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

true

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

true

Page 22: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

22 / 35July 1, 2010

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

__proto__

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

>>> dude.prototype??? // Trick question

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

true

Page 23: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

23 / 35July 1, 2010

__proto__ Chain

>>> typeof dude.numlegs

"undefined"

>>> Person.prototype.numlegs = 2;

>>> dude.numlegs

2

It is a live chain

Page 24: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

24 / 35July 1, 2010

Outline

Learning Tools Object and Functions Constructors and Prototype Inheritance Polymorphism

Page 25: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

25 / 35July 1, 2010

How?function NormalObject() { // Parent Constructorthis.name = 'normal';

this.getName = function() {

return this.name;

};

}

function PreciousObject(){ // child constructorthis.shiny = true;

this.round = true;

}

/*** How can we do this? ***/

var crystal_ball = new PreciousObject();

crystal_ball.name = 'Crystal Ball.';

crystal_ball.round; // true

crystal_ball.getName(); // "Crystal Ball."

Page 26: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

26 / 35July 1, 2010

Object Inheritance by Copying

// Two Objectsvar shiny = { shiny: true, round: true

}; var normal = { name: 'name me', getName: function() {

return this.name; }

};

// Inheritance tool functionfunction extend(parent,

child){

for (var i in parent) {

child[i] = parent[i];

}

}

// Inheritance by copyingextend(normal, shiny);

shiny.getName(); // "name me”

Page 27: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

27 / 35July 1, 2010

Prototypal Inheritance

function object(o) {

function F(){}

F.prototype = o;

return new F();

}

>>> var parent = {a: 1};

>>> var child = object(parent);

>>> child.a;

1

>>> child.hasOwnProperty(a);

false

Beget object

Page 28: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

28 / 35July 1, 2010

Outline

Learning Tools Object and Functions Constructors and Prototype Inheritance Polymorphism

Page 29: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

29 / 35July 1, 2010

Is the JavaScript an OOP Language?

Definitely sure! Object Oriented NOT Class Oriented

• Encapsulation• Inheritance• Polymorphism -- Because JavaScript is a dynamic language,

polymorphism is quite easy and very common. • Two common types of polymorphism:

• Runtime Replacement• Loadtime Branching

And it is more dynamic then Java and C++, these compiling languages

Page 30: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

30 / 35July 1, 2010

Loadtime Branchingvar getXHR = function () {

if (window.XMLHttpRequest) {

return function () {

// Return a standard XHR instance

};

}

else {

return function () {

// Return an Explorer XHR instance

};

}

}(); // Note: parents trigger self-invocation

Page 31: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

31 / 35July 1, 2010

Runtime Replacementvar documentListFactory = function () {

var out = []; // Just a simple array

// Override the default .push() method

out.push = function (document) {

Array.prototype.push.call(out, {

document : document,

timestamp : new Date().getTime()

});

};

return out;

};

Page 32: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

32 / 35July 1, 2010

Summary

Learning Tools• Firebug, JavaScript Shell

Object and Functions• JavaScript != Java• Object literal, Array literal, JSON• Functions: objects, invokable, return value

Constructors and Prototype• Constructor functions, constructor property• Built-in constructors, conventions• Prototype, __proto__ chain

Inheritance• By copying, prototypal

Polymorphism• Loadtime branching, runtime replacement

Page 33: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

33 / 35July 1, 2010

Exercises Write a JavaScript snippets in Firebug console defining

classes of Employee, Manager, and Secretary• Each Employee has a name and a salary• Each Manager is an Employee, and manages a group of other

Employees• Each Secretary is an Employee, and works for a Manager

add methods to these classes• Each Employee has a show() method returns her name and salary

as a string• Each Manager has a getInferiors() method returns her inferiors• Each Secretary has a getSuperior() method returns her boss

Inheritances in two different ways, copying and prototype

Page 34: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

34 / 35July 1, 2010

Further Readings

Introduction of JavaScript http://en.wikipedia.org/wiki/JavaScript

W3Schools JavaScript tutorial http://www.w3schools.com/js/default.asp

Mozilla Developer Center JavaScript documentation https://developer.mozilla.org/en/javascript

Object Oriented Programming in JavaScript by Mike Koss http://mckoss.com/jscript/object.htm

JavaScript Object-Oriented Programming Part 1 http://articles.sitepoint.com/article/oriented-programming-1

JavaScript Object-Oriented Programming Part 2 http://articles.sitepoint.com/article/oriented-programming-2

Page 35: Lecture 14 Object Oriented JavaScript · Every object in fact is a hash table internally (key: value) When a property is a function we can call it a method var obj = {}; obj.name

Web 2.0 Programming – Object-Oriented JavaScript

35 / 35July 1, 2010

Thank you!