Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prototypal Inheritance. Can We Do Inheritance Without Classes? How do we share behaviour across objects.

Similar presentations


Presentation on theme: "Prototypal Inheritance. Can We Do Inheritance Without Classes? How do we share behaviour across objects."— Presentation transcript:

1 Prototypal Inheritance

2 Can We Do Inheritance Without Classes? How do we share behaviour across objects

3 Name/Value Pairs Everything in JavaScript uses

4 Objects With Constructors function Foo(name) { this.name = name; } Foo.prototype = { alertName: function () { alert(this.name); } }; foo = new Foo("Jack”); foo.alertName();

5 .prototype vs.[[prototype]] [[prototype]] is a part of the ECMA standard and is not a property you directly access..prototype is only used with constructors. You can’t use it to switch prototypes on the fly.

6 What sets the prototype? new operator Object.create().prototype property of a constructor function Literals Regexp literal, function expression, array literal These objects all have their respective prototype object in their proto type chain.

7 Setting a Prototype with Object.create() var a = {a: 1}, b = Object.create(a); console.log(a.a) //outputs 1 console.log(b.a) // outputs 1 // because a is a prototype of b. console.log(a.isPrototypeOf(b)); //returns true

8 When referencing a property of an object, JavaScript first checks the the object then its prototype(s) until finally undefined is returned. Note the similarity of the Prototype Chain to the Scope Chain.


Download ppt "Prototypal Inheritance. Can We Do Inheritance Without Classes? How do we share behaviour across objects."

Similar presentations


Ads by Google