Trait.create with null prototype. Is it possible? Not recommended?

5 views
Skip to first unread message

Thiago F

unread,
Jan 20, 2016, 3:14:46 PM1/20/16
to traits.js
Hey, I just started using traits (but I waited for this moment a long time =P).

Usually a build my objects with prototype null. It does not seem possible using Trait.create.

Is it right?

(Is it here the right place to ask?)

Thank you.


Best,
Thiago F

Tom Van Cutsem

unread,
Jan 20, 2016, 3:21:05 PM1/20/16
to traits.js
It's definitely possible to use a null prototype with Trait.create.

Trait.create was explicitly modeled after ES5's built-in Object.create method. Both methods take as the first argument the prototype of the newly created object, and this is allowed to be null.

Example:

var Trait = require("traits.js");

var t1 = Trait({ a: 1, b: 2 })

var t2 = Trait({c: 3})

var t3 = Trait.compose(t1, t2)

var o = Trait.create(null, t3)

> o

{ a: 1, b: 2, c: 3 }

> o.hasOwnProperty // o doesn't inherit properties from Object.prototype

undefined


Cheers,

Tom

Thiago F

unread,
Jan 20, 2016, 7:01:44 PM1/20/16
to traits.js
Absolutely. My mistake. Thank you!

Cheers,
Thiago F
Reply all
Reply to author
Forward
0 new messages