It is possible to modidy an internal variable ?

12 views
Skip to first unread message

Olivier BONNAURE

unread,
Dec 2, 2010, 9:44:39 AM12/2/10
to traits.js
I'm doing something Like That :

function Game(nick, room, pos) {
return Trait.create( // create an instance of a trait
Object.prototype, // that inherits from Object.prototype
Trait.compose(
Trait({
socket: new io.Socket(null, {rememberTransport: false, port:
8090}),
nick: nick,
room: room,
pos: pos
}),
clone(TCoinche)
)
);
}

var game = new Game("oli", "1", "x");
game.nick = "sophie";
game.nick // still contain "oli"

It works fine on safari (mac) but not on google chrome (windows or
mac)

How can I modify this nick property ?

Tom Van Cutsem

unread,
Dec 2, 2010, 12:14:34 PM12/2/10
to trai...@googlegroups.com
Hello Olivier,

The assignment game.nick = "sophie" fails silently on chrome because it supports the new ES5 method Object.freeze, and because the game object returned by the call to Trait.create is frozen.

For example, try this in the web console:

var x = Object.freeze({ a: 2})
undefined
x
Object
x.a
2
x.a = 3
3
x.a
2

As you can see, assigning to a property of a frozen object fails silently and leaves the property unchanged.

If you really need a mutable nick property, change your call to Trait.create to Object.create. That will create a non-frozen trait instance. For the difference between Trait.create and Object.create, see the section called "Trait instantiation" of this article: http://howtonode.org/traitsjs

Cheers,
Tom

2010/12/2 Olivier BONNAURE <o.bon...@gmail.com>
Reply all
Reply to author
Forward
0 new messages