Download presentation
Presentation is loading. Please wait.
Published byScarlett Black Modified over 9 years ago
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.