1 / 8

Prototypal Inheritance

Prototypal Inheritance. How do we share behaviour across objects. Can We Do Inheritance Without Classes?. Everything in JavaScript uses. Name/Value Pairs. Objects With Constructors. function Foo(name) { this.name = name; } Foo. prototype = { alertName : function ( ) {

curt
Download Presentation

Prototypal Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Prototypal Inheritance

  2. How do we sharebehaviour across objects Can We Do Inheritance Without Classes?

  3. Everything in JavaScript uses Name/Value Pairs

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

  5. .prototypevs.[[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) untilfinally undefined is returned. Note the similarity of the Prototype Chain to the Scope Chain.

More Related