Inheritance 类_initialize()做什么?
我正在尝试学习如何创建createjs对象 我正在调查createjs/tutorials/heritary/demo.html和Button.jsInheritance 类_initialize()做什么?,inheritance,createjs,Inheritance,Createjs,我正在尝试学习如何创建createjs对象 我正在调查createjs/tutorials/heritary/demo.html和Button.js (function() { var Button = function(label, color) { this.initialize(label, color); } var p = Button.prototype = new createjs.Container(); // inherit from Container p.label;
(function() {
var Button = function(label, color) {
this.initialize(label, color);
}
var p = Button.prototype = new createjs.Container(); // inherit from Container
p.label;
p.background;
p.count = 0;
*p.Container_initialize = p.initialize;*
Button.prototype.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.addEventListener("click", this.handleClick);
this.addEventListener("tick", this.handleTick);
}
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;
}());
有一个自调用函数this.Container_initialize();
我试着把它注释掉,这使得代码无法工作。
有人能解释一下函数容器的初始化功能吗?
这是一个无限循环吗?这不是一个无限循环,发生的事情是你正在对旧循环进行“复制”(实际上只是一个新的引用)
p.Container_initialize = p.initialize;
这里,p.initialize
与createjs.Container.prototype.initialize
相同。
当你写作时:
Button.prototype.initialize = function(...) {
您正在覆盖容器.prototype.initialize
,但是由于您将其保存在容器中\u initialize
,因此您仍然可以调用它
至于函数做什么,这是阅读源代码的问题,它可能设置容器对象所需的任何内部内容。这就是为什么你不能叫它