Giter VIP home page Giter VIP logo

phina-patch-es-classes-support's Introduction

phina-patch-es-classes-support

このリポジトリについて

phina.js でビルトインクラスへの個別の細工なしに class 構文での継承を可能にするためのモンキーパッチです。

代わりに phina.createClass メソッドを書き換えてすべてのクラスを ES Classes 互換にします。

動作の確実な保証はできません。

インストール

このリポジトリをインストールします。

npm install negiwine/phina-patch-es-classes-support

次に phina.js を読み込む前にこのパッケージをインポートしてください。

require('phina-patch-es-classes-support')
const phina = require('phina.js')

使用例

パッケージの読み込み以外に特別な操作は不要です。

ただし phina.define のようにネームスペースへの登録は行わないので、シーンを作る場合や Element#fromJSON に対応させたい場合は個別に phina.register を使って関数呼び出し可能な形式で登録してください。

require('phina-patch-es-classes-support')
const phina = require('phina.js')

phina.globalize()


class Hero extends Sprite {
    constructor(){
        super('hero')
    }
}


class MainScene extends DisplayScene {
    constructor(){
        super()
        const hero = new Hero().addChildTo(this)
    }
}
phina.register('MainScene', (...args) => new MainScene(...args))

デコレータを使える場合は次のようなヘルパー関数も有効です。

const define = path => _class =>
    phina.register(path, (...args) => new _class(...args)) && _class
@define('MainScene')
class MainScene extends DisplayScene {
    constructor(){
        super()
        // ...
    }
}

作用

phina.createClass メソッドと superInit 及び superMethod メソッドを置き換えます。

phina.createClass で作られたクラスは通常通り関数呼び出し形式でのインスタンス化が可能です。ただし class 構文で継承したクラスは new 付きで呼び出す必要があり phina.createClass 関数で継承することもできません。

コードの簡単化のために superInit および superMethod メソッド を簡易的なものに置き換えています。パフォーマンスにほとんど影響はないはずですが使用はおすすめできません。 class 構文での定義では代わりに標準の super / super.methodName を使用してください。

License

WTFPL

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.