javascript - TypeError : this. initConfirmationSheet 不是一个函数

标签 javascript

我有以下 Javascript 函数:

function MyPseudoClass(){

     this.initConfirmationSheet = function initConfirmationSheet(myDiv){
         //foo
         //bar
         //etc.
      }


      //foo
     function setUpGUI(){
     var myDiv = document.getElementById(DivForDiagram);
     myDiv.innerHTML = ""; //Clear contents of DIV for subsequent Diagram drawing. 
     confSheetDiagramRef = GO(go.Diagram, DivForDiagram,  // the DIV HTML element
    {
      initialContentAlignment: go.Spot.Center,
      initialAutoScale:go.Diagram.Uniform,
    "toolManager.mouseWheelBehavior": go.ToolManager.WheelZoom
    });
    confSheetDiagramRef.initialContentAlignment = go.Spot.Center;
    confSheetDiagramRef.undoManager.isEnabled = true;
    confSheetDiagramRef.isReadOnly = true;


    var texttemplate = GO(go.Node, "Horizontal", 
                      GO(go.TextBlock,
                                                new go.Binding("text", "text"), 
                                                new go.Binding("font", "fontType"),
                                                    new go.Binding("textAlign", "textAlign")
                                                ),
                                                    new go.Binding("location", "shapePosition"),
                                                    new go.Binding("angle", "rotation"),
                                                  new go.Binding("width", "width"), 
                                                  new go.Binding("height", "height")

                                            )


            var imagetemplate = GO(go.Node, "Horizontal", 
                      GO(go.Picture,
                                                new go.Binding("text", "text"), 
                                                new go.Binding("font", "fontType"), 
                                                  new go.Binding("location", "position"),
                                                    new go.Binding("angle", "rotation"),
                                                  new go.Binding("width", "width"),
                                                    new go.Binding("source", "text"),
                                                  new go.Binding("height", "height"),
                                                    new go.Binding("maxSize", "bounds")
                                                )
                                            )


var templmap = new go.Map("string", go.Node);
templmap.add("text", texttemplate);
templmap.add("image", imagetemplate);
confSheetDiagramRef.nodeTemplateMap = templmap;

    confSheetDiagramRef.addDiagramListener('InitialLayoutCompleted', function(e){
          alert("in listener!");
        generateImages(700,100);                                                                                                                                                                        
    })
    }

     function generateImages(){
          //foo
          //bar
          this.initConfirmationSheet('myDiv');
      }

 }

MyPseudoClass在以下上下文中调用:

    nextConf = new MyPseudoClass(this);
            nextConf.setInfo(confInfoVO, true, PasseDInEventDateTime_str, ParentRef.TicketPrintLayout, false);
            nextConf.printAllConfirmations(ParentRef.DivName, numConfirmations);

我收到以下错误: 类型错误:this.initConfirmationSheet 不是函数

我不确定为什么第一个函数不在第二个函数的范围内。 特别是因为第一个函数可用于 MyPseudoClass 内的另一个函数。 :

this.thirdFunction = function thirdFunction(divName){
    this.initConfirmationSheet(divName);
 }

是因为这个函数被定义了,还是附加到了这个类的实例上?与 this.functionName 未定义的另一个相反?

最佳答案

您不受 MyPseudoClass 的约束例如,当您从外部来源调用 <a onclick="myPseudoClassObj.generateImages()> 时,您可以通过执行以下操作强制绑定(bind)所有调用

 this.generateImages = function(){
      //foo
      //bar
      this.initConfirmationSheet('myDiv');
  }.bind(this);

see fiddle

关于javascript - TypeError : this. initConfirmationSheet 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33247945/

相关文章:

多个类的Javascript点击功能

javascript - 使用地理位置更改货币

javascript - jQuery Widgets 无法在 Ajax 加载的选项卡上运行

javascript - 如何获得缺失的坐标以使我的线垂直于另一条线?

javascript - 如何将 window.setTimeout 与 javascript 和模块模式一起使用

javascript - 来自自定义格式字符串的 Moment js 格式

javascript - Firebase 函数无法读取未定义的属性 'val'

javascript - 如何使用nodeJS下载文件

javascript - 为什么我的内部函数在我的简单 Javascript OOP 游戏中返回为未定义

javascript - NodeJS 基本文件 IO