Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 私有变量正在跨类的多个实例共享_Javascript_Jquery_Inheritance_Prototypal Inheritance_Draw2d Js - Fatal编程技术网

Javascript 私有变量正在跨类的多个实例共享

Javascript 私有变量正在跨类的多个实例共享,javascript,jquery,inheritance,prototypal-inheritance,draw2d-js,Javascript,Jquery,Inheritance,Prototypal Inheritance,Draw2d Js,我在JavaScript函数中使用私有变量时遇到了一个小问题。该软件依赖于draw2d,一个javascript渲染库。该库迫使您使用John Resig的实现,它允许简单的类Java继承,我认为这是问题的根源 //= require block/XBlock.js console.log("sblock loaded."); xmicro.block.SimpleBlock = xmicro.block.XBlock.extend({ NAME: "xmicro.block.Simpl

我在JavaScript函数中使用私有变量时遇到了一个小问题。该软件依赖于draw2d,一个javascript渲染库。该库迫使您使用John Resig的实现,它允许简单的类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上下文菜单。properties对象中有一个名为
build
的方法,我使用
buildFunc
截取调用并附加调用它的图形的id

问题是,如果我声明
var supaId=this.id
,当我制作更多此类图形时,它们开始共享supaId,并开始交叉污染变量。另一个问题是,如果我像下面的I一样只使用
supaId=this.id
,那么它可以工作,但会将supaId放在全局范围内。这两个都不是我的好选择,我想知道如何修复它


感谢您的帮助

John Resig的
课程
绝不是强制的。它只是增加了语法上的糖分,您也可以用“正常”的方式进行继承。不,
var supaId
是正确的。如果您创建的
buildfunc
中有一个“共享”它,是否只有一个会被触发?一定是出了什么问题。
image=path
可能应该是
this.image=path你不需要全局
图像
变量。是的@Bergi,这是我之前使用的东西的一个片段。当右键单击地物的实例时,会触发onContextMenu,因此它会运行特定于实例的onContextMneu代码,该代码反过来调用buildFunc。我相信它不会被多次触发。@Bergi,刚刚确认
buildFunc
每次点击都会运行一次。