[英]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.