Skip to content

Commit

Permalink
Simplify check if inheriting from a non-Pjs class
Browse files Browse the repository at this point in the history
If proto.init is truthy but not a function, like 1 or something, you'll
get a

    TypeError: Object 1 has no method 'apply'

which I feel like is excellent behavior.

If we're inheriting from a Pjs class but `proto.init` is overridden to a
falsy value, we'll think we're inheriting from a non-Pjs class and set
`proto.init` to the superclass constructor, which isn't the most
defensive, fail-early thing to do, but is also what happened prior to
this commit.

Really, to be defensive in our non-pjs check, we should check
`('init' in proto)`, so if `proto.init` is overridden to anything
but a function it'll TypeError like above.
  • Loading branch information
jneen committed Oct 28, 2013
1 parent 66714fa commit e97eb0b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ tom.move()

## how is pjs different from X

Most class systems for JS let you define classes by passing an object. P.js lets you pass a function instead, which allows you to closure private methods and macros. It's also <0.4kb minified (`make report`: 498).
Most class systems for JS let you define classes by passing an object. P.js lets you pass a function instead, which allows you to closure private methods and macros. It's also <0.4kb minified (`make report`: 478).

### why doesn't pjs suck?

Expand Down
8 changes: 3 additions & 5 deletions src/p.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,9 @@ var P = (function(prototype, ownProperty, undefined) {
}
}

// if there's no init, we assume we're inheriting a non-pjs class, so
// we default to applying the superclass's constructor.
if (typeof proto.init !== 'function') {
proto.init = _superclass;
}
// if no init, assume we're inheriting from a non-Pjs class, so
// default to using the superclass constructor.
proto.init = proto.init || _superclass;

return C;
})(definition);
Expand Down

0 comments on commit e97eb0b

Please sign in to comment.