demystifying prototypes

30
types Demystifying Proto ...........

Upload: dmitry-baranovskiy

Post on 15-May-2015

10.665 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Demystifying Prototypes

typesDemystifyingProto...........

Page 2: Demystifying Prototypes

types

Page 3: Demystifying Prototypes

undefined numbernull

boolean objectstring

Page 4: Demystifying Prototypes

undefined numbernull

boolean objectstring

Page 5: Demystifying Prototypes

b.name

b[“name”]

“a” in b

for (var i in b) …

b.hasOwnProperty(“a”)

b instanceof d

Page 6: Demystifying Prototypes
Page 7: Demystifying Prototypes

function f() {};

f();new f;

function

Page 8: Demystifying Prototypes

function f() {};

f();new f;

function

Page 9: Demystifying Prototypes

function f() {};

f();new f;

function

Page 10: Demystifying Prototypes

ObjectNumberStringBooleanFunctionArrayRegExpDate

Page 11: Demystifying Prototypes

A

function A() {};

Page 12: Demystifying Prototypes

A

function A() {};

constructor: A

Page 13: Demystifying Prototypes

A

b

function A() {};

constructor: A

var b = new A;

Page 14: Demystifying Prototypes

A

b

x: 1

function A() {};

constructor: A

var b = new A;A.prototype.x = 1;

Page 15: Demystifying Prototypes

A

b

x: 1

y: 2

function A() {};

constructor: A

var b = new A;A.prototype.x = 1;b.y = 2;

Page 16: Demystifying Prototypes

A

bc

x: 1

y: 2

function A() {};

constructor: A

var b = new A;A.prototype.x = 1;b.y = 2;var c = new A;

Page 17: Demystifying Prototypes

A

bc

x: 1

y: 2

function A() {};

constructor: A

var b = new A;A.prototype.x = 1;b.y = 2;var c = new A;A.prototype = {v: 1, w: 2};

Page 18: Demystifying Prototypes

A

bc

x: 1

y: 2

v: 1w: 2

function A() {};

constructor: A

var b = new A;A.prototype.x = 1;b.y = 2;var c = new A;A.prototype = {v: 1, w: 2};

Page 19: Demystifying Prototypes

A

bc

x: 1

y: 2d

v: 1w: 2

function A() {};

constructor: A

var b = new A;A.prototype.x = 1;b.y = 2;var c = new A;A.prototype = {v: 1, w: 2};var d = new A;

Page 20: Demystifying Prototypes

A P

bc

x: 1

y: 2d

v: 1w: 2

constructor: A

P.hasOwnProperty(“constructor”)“constructor” in cfor (var i in P) alert(i);c.constructorc instanceof Ad instanceof Ad.constructor

Page 21: Demystifying Prototypes

null

d

A

Object

c

Page 22: Demystifying Prototypes

function A() {};function B() {};B.prototype = new A;var d = new B;

null

d

A

Object

B

Page 23: Demystifying Prototypes

null

d

A

Object

B

function A() {…};function B() {};var Z = function () {};Z.prototype = A.prototype;B.prototype = new Z;var d = new B;

Page 24: Demystifying Prototypes

null

d

A

Object

B

function A() {…};function B() {};var Z = function () {};Z.prototype = A.prototype;B.prototype = new Z;var d = new B;

Z

Page 25: Demystifying Prototypes

goog.inherits = function(childCtor, parentCtor) { /** @constructor */ function tempCtor() {}; tempCtor.prototype = parentCtor.prototype; childCtor.superClass_ = parentCtor.prototype; childCtor.prototype = new tempCtor(); childCtor.prototype.constructor = childCtor;};

Page 26: Demystifying Prototypes

exports.inherits = function(ctor, superCtor) { ctor.super_ = superCtor; ctor.prototype = Object.create(superCtor.prototype, { constructor: { value: ctor, enumerable: false } });};

Page 27: Demystifying Prototypes

null

A

Function

Object

Page 28: Demystifying Prototypes

null

A

Function

Object

Page 29: Demystifying Prototypes

Function.constructor === Function

JavaScript Paradox