Javascript foundations: Classes and `this`

15
Classes and ‘this’ Learning Javascript foundations John Hunter 2 June 2009 1

description

This presentation explains the ‘this’ property and how class based inheritance can be emulated. The presentation forms part of a series on learning Javascript foundations.

Transcript of Javascript foundations: Classes and `this`

Page 1: Javascript foundations: Classes and `this`

Classes and ‘this’Learning Javascript foundations

John Hunter2 June 2009

1

Page 2: Javascript foundations: Classes and `this`

object propertiesUnlike scope, properties assigned to an object do not have lexical context. They belong to the object they are assigned to.

However, they can be assigned to more than one object...and they can be assigned very late.

2

Page 3: Javascript foundations: Classes and `this`

getName

radio1

radio2 this

this

When the function is executed the 'this' property is assigned the object from which it was called.

Functions are data - they can be assigned as properties of multiple objects (i.e. multiple references).

radio1.getName();

radio2.getName();

3

Page 4: Javascript foundations: Classes and `this`

function getName () { console.log(this.name);}

window.name = 'Global';getName();

var radio1 = { name: 'radio one', getName: getName};radio1.getName();

Function returns the name property of this.

getName called in the window (global) context

getName called in the radio1 context

>>> Global

>>> radio one

4

Page 5: Javascript foundations: Classes and `this`

var radio1 = { name: 'radio one', getName: getName};radio1.getName();

var radio2 = { name: 'radio two', getName: getName};radio2.getName();

getName called in the radio2 context

>>> radio one

>>> radio two

getName called in the radio1 context

5

Page 6: Javascript foundations: Classes and `this`

ClassesIn class based programming the class is a blueprint for objects. First a class is defined and then instances of class can be created.

Javascript does not directly support classes but can emulate them.

6

Page 7: Javascript foundations: Classes and `this`

ClassesA class definition generally consists of:

- a constructor

- methods

Many languages have an explicit class definition that defines both. In Javascript these are defined separately using functions.

7

Page 8: Javascript foundations: Classes and `this`

Constructor functionA constructor function is a function that:

1. creates an instance object when called with new

2. assigns properties to the new object this

3. provides access to the prototype of the object

Note: Javascript cannot know which functions are going to be used as constructors so all functions support these features.

8

Page 9: Javascript foundations: Classes and `this`

// Constructorfunction ClassName (propertyValue) { this.instanceProperty = propertyValue;}

// create an instance of the Classvar instance = new ClassName('my instance property');console.log(instance. instanceProperty);

>>> my instance property

9

Page 10: Javascript foundations: Classes and `this`

function getName () { console.log(this.name);}

// Constructorfunction RadioClass (name) { this.name = name; this.getName = getName;}

var radio2 = new RadioClass('radio two');radio2.getName();

constructor binds getName function as an

instance property

getName returns the instance property

>>> radio two

10

Page 11: Javascript foundations: Classes and `this`

// Constructorfunction RadioClass (name) { this.name = name; this.getName = function () { console.log(this.name); };}

// create an instance of the Classvar radio3 = new RadioClass('radio three');radio3.getName();

>>> radio three

method is added as an instance property

✖ duplicated with each instance created

11

Page 12: Javascript foundations: Classes and `this`

// Constructorfunction RadioClass (name) { this.name = name;}

RadioClass.prototype.getName = function () { console.log(this.name);};

// create an instance of the Classvar radio3 = new RadioClass('radio three');radio3.getName();

>>> radio three

method assigned to prototype object is inherited

by all instances

new creates a new this object which inherits from the constructor prototype

12

Page 13: Javascript foundations: Classes and `this`

radio3.getName();

RadioClass.prototype

getName ()

radio1

name = 'radio one'

radio2

name = 'radio two'

radio3

name = 'radio three'

2) look here

1) look here

RadioClass constructor

this.name

new3) get this.name

13

Page 14: Javascript foundations: Classes and `this`

- this refers to the calling context of a function

- this is set at function invocation

- classes can be emulated using constructor functions

- calling new with a constructor returns an instance of this

- function.prototype provides an inheritance mechanism for instances to share methods

Review

14

Page 15: Javascript foundations: Classes and `this`

Thanks

15