简体   繁体   English

未捕获的TypeError:undefined不是函数

[英]Uncaught TypeError: undefined is not a function

Button.js: Button.js:

(function () {
    function Button(label) {
        console.log(label);
    }
}());

demo.html: 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>

for the line button = new createjs.Button("Anything"); 对于行button = new createjs.Button(“ Anything”); Error: Uncaught TypeError: undefined is not a function Why i am getting this error? 错误:未捕获TypeError:未定义不是函数为什么我收到此错误?

It is because the Button is not in a global scope or window . 这是因为Button不在全局范围或window

You don't need IEFE here. 您在这里不需要IEFE。 Remove it: 去掉它:

function Button(label) {
   console.log(label);     
}

If you want it with IEFE, then do this: 如果要使用IEFE,请执行以下操作:

$(function () {
    window.Button = function (label) {
        console.log(label);
    }
});

If you want to access Button by using createjs.Button , then you must create Button on the createjs object: 如果要使用createjs.Button访问Button ,则必须在createjs对象上创建Button

createjs.Button = function(label){
    console.log(label);
};

The above will work in any scope, including the global and your function expression, because createjs is in the global scope. 上面的代码可以在任何范围内工作,包括全局和函数表达式,因为createjs在全局范围内。 Now you can do: 现在您可以执行以下操作:

button = new createjs.Button("Anything");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM