search

Home  >  Q&A  >  body text

javascript - How to write this function? What are the advantages?

var CanvasDraw = {
        canvas:null,//canvas元素
        context:null,//canvas环境
        fps:30,//帧频
        type:null, //类型 letter或 img 对象
        drawObj:null,
        gap:null,
        dotsData:[],
        dots:[],

        Init:function(canvas,width,height,type,drawObj,gap,fps,fn){
            
            CanvasDraw.canvas = canvas;
            CanvasDraw.context = canvas.getContext("2d");
            
            CanvasDraw.canvas.width=width;
            CanvasDraw.canvas.height=height;
                        
            CanvasDraw.fps = fps || 30;
            CanvasDraw.type = type || "letter";
            CanvasDraw.gap=gap||10;
                
            CanvasDraw.drawObj=drawObj;
            CanvasDraw.dotsData=[];
            CanvasDraw.dots=[];
            
            CanvasDraw.ShapeBuilder.Init();    
           },    
         Render:function(canvas,width,height,type,drawObj,fps,fn){
            this.Init(canvas,width,height,type,drawObj,fps,fn);
         },
         StopRender:function(fn){
                
             }
         
}

Also, is this a constructor?

大家讲道理大家讲道理2862 days ago465

reply all(6)I'll reply

  • 天蓬老师

    天蓬老师2017-05-19 10:12:57

    Object-oriented programming, if there are any advantages to this way of writing, avoid polluting global variables

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-19 10:12:57

    This is a method of an object

    reply
    0
  • 阿神

    阿神2017-05-19 10:12:57

    Object literal function

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-19 10:12:57

    Object literal writing method, this code creates a CanvasDraw object, not a constructor.

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-19 10:12:57

    Thanks for the invitation! This is object-oriented programming. It's not a constructor, the constructor is a method, you are just an object.

    reply
    0
  • 阿神

    阿神2017-05-19 10:12:57

    The one with new is the constructor. This is how you write the object literal

    reply
    0
  • Cancelreply