[英]Uncaught TypeError: undefined is not a function
Button.js:
(function () {
function Button(label) {
console.log(label);
}
}());
demo.html:
<html>
<head>
<script src="../../lib/easeljs-0.7.1.min.js"></script>
<script src="Button.js"></script>
<script>
window.onload = function() {
var canvas, stage, button;
canvas = document.getElementById("canvas");
stage = new createjs.Stage(canvas);
button = new createjs.Button("Anything");
stage.addChild(button);
stage.update();
}
</script>
</head>
<body>
<canvas id="canvas" width=200 height=200>Canvas is not supported</canvas>
</body>
</html>
对于行button = new createjs.Button(“ Anything”); 错误:未捕获TypeError:未定义不是函数为什么我收到此错误?
这是因为Button
不在全局范围或window
。
您在这里不需要IEFE。 去掉它:
function Button(label) {
console.log(label);
}
如果要使用IEFE,请执行以下操作:
$(function () {
window.Button = function (label) {
console.log(label);
}
});
如果要使用createjs.Button
访问Button
,则必须在createjs
对象上创建Button
:
createjs.Button = function(label){
console.log(label);
};
上面的代码可以在任何范围内工作,包括全局和函数表达式,因为createjs
在全局范围内。 现在您可以执行以下操作:
button = new createjs.Button("Anything");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.