在kineticjs中的处理程序函数中使用全局变量

Use a global variable inside a handler function in kineticjs

本文关键字:函数 全局变量 处理 kineticjs 程序      更新时间:2023-09-26

我在javascript文件的开头声明了一个名为analysis的全局函数

然后我有一个函数,它被用作contentClicked on stage事件的处理程序。

//global
var analysis
function onStageContentClicked(event){
    //I use analysis
    var pointId = "a point name";
    if (analysis.hasPoint(pointId)){
        //....do some more things
    }
    //...and more...
}

$(document).ready(function(){
    if ($("select").val() === "Downs"){
         analysis = Downs(23);
        stage.on("contentClicked", onStageContentClicked);
    }
});

Inside on StageContentClickd on if part I get a cannot find hasPoint of undefined。这意味着我的Downs对象未设置。我在另一个名为analysis.js的js文件中创建了一个Downs类

function Downs (maxPoints){
    this.points = {};
    this.maxPoints = maxPoints
    this.go_apostrophe;
    this.go_apoAngle;
    this.bicetrix = [];

}

Downs.prototype.addPoint = function (pos, pointId){
    this.points[pointID] = {'x':pos.x, 'y':pos.y};
}
Downs.prototype.pointExists = function (pointId){
    var pointIds = Object.keys(this.points);
    for (var i=0; i< pointIds.length; i++){
        if (pointId === pointIds[i]){
            return true;
        }
    }
    return false;
}
//etc

但我无法创建对象。我打赌我在用calss对象做错事。你能帮我吗?我在包含主文档.ready代码的js文件之前添加了我的analysis.js。

Downs是一个构造函数,因此需要使用new:调用它

analysis = new Downs(23);