[traits.js] Confused

13 views
Skip to first unread message

Nathan

unread,
May 8, 2010, 3:50:07 PM5/8/10
to traits.js
I'm receiving some confusing behavior.

Here is an example:

var obj = Trait.create({ hello: "world" }, Trait({ toString:
function() { return this.hello; } }));
obj.toString(); // -> "world"

var obj = Trait.create({ hello: "world" }, Trait({
hello: Trait.required,
toString: function() { return this.hello; }
}));
obj.toString(); // -> this.hello is undefined

I don't understand why marking it as required makes it undefined when
I provide hello via the prototype in the same way.

Tom Van Cutsem

unread,
May 10, 2010, 9:42:47 AM5/10/10
to trai...@googlegroups.com
Hi Nathan,

You just discovered the first bug in traits.js v0.1 ;-)

The bug is fixed in the head release:

With the fix, your example will return 'world' for both cases, as it should.

Cheers,
Tom

2010/5/8 Nathan <nrs...@gmail.com>

Nathan Stott

unread,
May 10, 2010, 10:00:13 AM5/10/10
to trai...@googlegroups.com
Yay, first bug ^^

Thanks for the fix!

Nathan Stott

unread,
May 10, 2010, 10:12:58 AM5/10/10
to trai...@googlegroups.com
Tom,

I am using this Trait as a CommonJS module.  It would be excellent if you added support for this by adding at the end of the file

if (exports) {
    exports.Trait = Trait;

Tom Van Cutsem

unread,
May 10, 2010, 10:32:43 AM5/10/10
to trai...@googlegroups.com
Done. I did use a slightly different feature-test as undefined free variables result in exceptions on some JS engines:

if (typeof exports !== "undefined") { // CommonJS module support
  exports.Trait = Trait;
}

2010/5/10 Nathan Stott <nrs...@gmail.com>

Nathan Stott

unread,
May 10, 2010, 10:42:10 AM5/10/10
to trai...@googlegroups.com
Perfect, thanks

Mark S. Miller

unread,
May 10, 2010, 11:16:51 AM5/10/10
to trai...@googlegroups.com
On Mon, May 10, 2010 at 7:32 AM, Tom Van Cutsem <tvcu...@gmail.com> wrote:
Done. I did use a slightly different feature-test as undefined free variables result in exceptions on some JS engines:

Hopefully all. That exception is required by the spec. Your test is the correct fix.

Nathan, thanks for the report!



--
    Cheers,
    --MarkM

Nathan Stott

unread,
May 10, 2010, 3:36:23 PM5/10/10
to trai...@googlegroups.com
No problem, I'm enjoying using traits
Reply all
Reply to author
Forward
0 new messages