0

我有一段 javascript 代码,我想为其添加命名空间。在此代码中,在函数之外发生了赋值操作。有人可以告诉我如何将它放在命名空间中吗?代码如下。

var mynameSpace={
canvasPanel:{},
stage:{},
someShape:{},

drawLineGraph:function(dataList,color,baseY)
{
//Create a shape
this.dataList=dataList;
this.index=0;
this.currentDay=1;
},

myNameSpace.drawLineGraph.prototype = new createjs.Shape(); //Getting the problem here
myNameSpace.drawLineGraph.prototype.constructor = drawLineGraph; //Getting the problem here**
,
drawLegend:function(){
}

};
4

3 回答 3

1

您可以将函数和后续分配包装在另一个函数中,然后立即调用它,如下所示:

drawLineGraph: (function() {
   var f = function(dataList, color, baseY {
       // Create a shape
          ... function code ...
   };
   f.prototype = new createjs.Shape();
     ... more assignments ...

   return f; // this will be assigned to drawLineGraph
})(),
于 2013-04-18T01:50:38.093 回答
0

只需替换drawLineGraphthis.drawLineGraph.

于 2013-04-18T01:46:10.140 回答
0

看起来您正在对象文字定义中进行原型分配。因此,当您尝试修改它的原型时,mynameSpace 的 drawLineGraph 属性不存在。将 drawLineGraph 原型属性分配移到对象文字下方。

var mynameSpace={
    canvasPanel:{},
    stage:{},
    someShape:{},

    drawLineGraph:function(dataList,color,baseY)
        {
        //Create a shape
        this.dataList=dataList;
        this.index=0;
        this.currentDay=1;
    },

    drawLegend:function(){
    }

};
myNameSpace.drawLineGraph.prototype = new createjs.Shape(); //No longer a problem
myNameSpace.drawLineGraph.prototype.constructor = drawLineGraph; //No longer a problem
于 2013-04-18T01:54:17.860 回答