0

JavaScript 関数でプライベート変数を使用する際に小さな問題があります。このソフトウェアは、javascript のレンダリング ライブラリである draw2d に依存しています。このライブラリでは、John Resig のClass.jsの実装を使用する必要があります。これにより、単純な Java のようなクラス継承が可能になり、問題の根源であると私は信じています。

//= require block/XBlock.js
console.log("sblock loaded.");
xmicro.block.SimpleBlock = xmicro.block.XBlock.extend({
    NAME: "xmicro.block.SimpleBlock",

    init: function(path, width, height,input,output,contextMenua){
        this._super(path,width,height,input,output,contextMenua);
        image = path;
        this.contextMenu = contextMenua;
        xmicro.istn.BlockAttributeStore[this.id] = new xmicro.block.BlockAttributeStore();
    },
    getImage: function () {
        return this.image;
    },

    onContextMenu: function (x, y) {
        supaId = this.id;
        that = this;
        var buildFunc = function(){
            var args = Array.prototype.slice.call(arguments);
            args.push(supaId);
            return that.contextMenu.build.apply(null, args);    
        };
        if(this.contextMenu !== undefined){
            $.contextMenu({
                trigger: "right",
                selector: this.contextMenu.selector,
                build: buildFunc
            });
        }
    },

    getPersistentAttributes: function () {
        var memento = this._super();

        return memento;
    }
});

上部のコメントは自動連結を行いますが、問題の焦点はonContextMenu関数にあります。プロパティ オブジェクトから jQuery コンテキスト メニューを作成するために呼び出す必要があるクロージャーがあります。プロパティ オブジェクト内の 1 つは、 と呼ばれるメソッドbuildです。これを使用buildFuncして、呼び出しをインターセプトし、呼び出された Figure の ID を追加します。

問題は、私が を宣言するvar supaId = this.idと、このタイプの図をさらに作成すると、それらは supaId を共有し始め、変数の相互汚染を開始することです。もう1つの問題は、supaId = this.id以下のように使用すると機能しますが、グローバルスコープにsupaIdを配置することです。これらはどちらも私にとっては良い選択肢ではありません。修正方法を理解したいと思います。

どんな助けでも大歓迎です!

4

0 に答える 0