看似递归的不是递归,是缓存之前的函数
看似递归的不是递归,是缓存之前的函数
(function () { var Button = function(label, color){ this.initialize(label, color); }; var p = Button.prototype = new createjs.Container(); p.label; p.background; p.count = 0; p.Container_initialize = p.initialize; // 这个是缓存之前的函数; p.initialize = function(label,color){ //重新定义了这个函数; this.Container_initialize(); //调用了之前缓存的函数 this.label = label; if(!color) { color ='#ccc'; } var text = new createjs.Text(label, '20px Arial','#000'); text.textBaseline = 'top'; text.textAlign = 'center'; var width = text.getMeasuredWidth() + 30; var height = text.getMeasuredHeight() + 20; this.background = new createjs.Shape(); this.background.graphics.beginFill(color).drawRoundRect(0,0,width,height,10); text.x = width /2; text.y = 10; this.addChild(this.background, text); this.on('click', this.handleClick); this.on('tick', this.handleTick); this.mouseChildren = false; }; p.handleClick = function(event){ var target = event.target; alert('You clicked on a button: '+ target.label); }; p.handleTick = function (event) { p.alpha = Math.cos(p.count++*0.1)*0.4+0.6; }; window.Button = Button; }());