10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaScriptのサブクラス定義チートシート

Last updated at Posted at 2014-02-01

チートシートなので詳細な説明一切なしです。

サブクラスの書き方。 ClassNameというクラスは定義済みとします。

var SubClassName = (function () { // subclass
    function SubClassName() { // public constructor
        // call superclass constructor
        ClassName.call(this, "paramValue");

        // add public instance property
        this.subPropName = "subPropValue";
    }
    // configure prototype
    SubClassName.prototype = new ClassName();
    SubClassName.prototype.constructor = SubClassName;
    
    // add public instance method
    SubClassName.prototype.subFuncName = function () {
    };

    return SubClassName; // return constructor
})();

// test instanceof
var instance = new ClassName("paramValue");
var subInstance = new SubClassName();
alert(instance instanceof ClassName); // true
alert(instance instanceof SubClassName); // false
alert(subInstance instanceof ClassName); // true
alert(subInstance instanceof SubClassName); // true
10
10
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
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?