tvcutsem / es-lab

Automatically exported from code.google.com/p/es-lab
23 stars 10 forks source link

Required trait properties override inherited prototype properties with undefined #10

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Bug reported by Nathan on the traits.js mailing list:

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

Original issue reported on code.google.com by tvcut...@gmail.com on 10 May 2010 at 1:30

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r412.

Original comment by tvcut...@gmail.com on 10 May 2010 at 1:31

GoogleCodeExporter commented 9 years ago
The problem was due to the way Trait.create deals with required properties:

      if (pd.required) && !(name in proto)) {
          throw new Error('Missing required property: '+name);
      } else ...

In this case, properties present in 'proto' will not trigger an Error, but 
rather than skipping pd altogether, the 
code will continue in the else branch.

Changing the logic as follows fixes this problem:

      if (pd.required) {
        if (!(name in proto)) {
          throw new Error('Missing required property: '+name);
        }

Fixed in r412.

Original comment by tvcut...@gmail.com on 10 May 2010 at 1:33