JavaScript: The Good Parts Part Four: Prototypal Inheritance - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

JavaScript: The Good Parts Part Four: Prototypal Inheritance

Description:

var newObject = Object.create(oldObject); newObject.thirdMethod = function ... Using the Object.create method, we can quickly produce new objects that have the ... – PowerPoint PPT presentation

Number of Views:209
Avg rating:3.0/5.0
Slides: 18
Provided by: douglasc
Category:

less

Transcript and Presenter's Notes

Title: JavaScript: The Good Parts Part Four: Prototypal Inheritance


1
JavaScript The Good PartsPart Four Prototypal
Inheritance
  • Douglas Crockford
  • douglas_at_crockford.com

2
Prototypal Inheritance
  • Some languages have classes, methods,
    constructors, and modules. JavaScript's functions
    do the work of all of those.
  • Instead of Classical Inheritance, JavaScript has
    Prototypal Inheritance.
  • It accomplishes the same things, but differently.
  • It offers greater expressive power.
  • But it's different.

3
Prototypal Inheritance
  • Instead of organizing objects into rigid classes,
    new objects can be made that are similar to
    existing objects, and then customized.
  • Object customization is a lot less work than
    making a class, and less overhead, too.
  • One of the keys is the Object.create() method.
  • The other key is functions.

4
Object.create
  • if (typeof Object.create ! 'function')
  • Object.create function (o)
  • function F() F.prototype o return
    new F()

5
Prototypal Inheritance
  • Class-free. Objects inherit from objects.
  • An object contains a secret link to another
    object.
  • Mozilla calls it __proto__.
  • var newObject
  • Object.create(oldObject)

newObject
oldObject
6
Prototypal Inheritance
  • var oldObject
  • firstMethod function () ...,
  • secondMethod function () ...
  • var newObject Object.create(oldObject)
  • newObject.thirdMethod function () ...
  • var myDoppelganger Object.create(newObject)
  • myDoppelganger.firstMethod()

7
Prototypal Inheritance
  • If an object has a foo property, then the chain
    will not be consulted when accessing member foo.
  • newObject.foo newObject'foo'

newObject
oldObject
8
Prototypal Inheritance
  • If access of a member of newObject fails, then
    search for the member in oldObject.
  • If that fails, then search for the member in
    Object.prototype.

newObject
oldObject
9
Prototypal Inheritance
  • Changes in oldObject may be immediately visible
    in newObject.
  • Changes to newObject have no effect on oldObject.

newObject
oldObject
10
Prototypal Inheritance
  • oldObject can be the prototype for an unlimited
    number of objects which will all inherit its
    properties.

newObject
oldObject
11
Prototypal Inheritance
  • newObject can be the prototype for an unlimited
    number of even newer objects.
  • There is no limit to the length of the chain
    (except common sense).

myDoppelganger Object.create(newObject)
newObject
oldObject
12
Augmentation
  • Using the Object.create method, we can quickly
    produce new objects that have the same state and
    behavior as existing objects.
  • We can then augment each of the instances by
    assigning new methods and members.

13
Object.create
  • if (typeof Object.create ! 'function')
  • Object.create function (o)
  • function F() F.prototype o return
    new F()

14
Object.create function
  • Object.create function (o)
  • function F()
  • F.prototype o
  • return new F()
  • newobject Object.create(oldobject)

F
15
Object.create function
  • Object.create function (o)
  • function F()
  • F.prototype o
  • return new F()
  • newobject Object.create(oldobject)

F
oldobject
16
Object.create function
  • Object.create function (o)
  • function F()
  • F.prototype o
  • return new F()
  • newobject Object.create(oldobject)

F
newobject
oldobject
17
Object.create function
  • Object.create function (o)
  • function F()
  • F.prototype o
  • return new F()
  • newobject Object.create(oldobject)

newobject
oldobject
Write a Comment
User Comments (0)
About PowerShow.com