LoginSignup
2
2

More than 5 years have passed since last update.

CoffeeScriptでサブクラスを判定

Last updated at Posted at 2014-08-14

SubClass.prototype instanceof SuperClass で判定します。

検証コード:

assert = require 'assert'

Foo = class
Bar = class extends Foo
Baz = class extends Bar
Hoge = class

assert(Bar.prototype instanceof Foo)
assert(Baz.prototype instanceof Bar)
assert(Baz.prototype instanceof Foo)
assert(Foo.prototype instanceof Foo is false)
assert(Hoge.prototype instanceof Foo is false)

assert(Foo.prototype instanceof Object)
assert(Foo.prototype instanceof Array is false)

なお、これは Node.js の util.inherits で作成したサブクラスの判別方法と同じです。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2