首页 > js创建自定义类时报错

js创建自定义类时报错

js部分:

;(function($){
    //声明LightBox类构造函数 
    var LightBox = function(){ 
        alert('msg'); 
         
    };

    LightBox.prototype = { 

    };
        Window["LightBox"] = LightBox;
})(jQuery);

html部分:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" type="text/css" href="css/reset.css">
    <link rel="stylesheet" type="text/css" href="css/lightbox.css"> 
</head>
<body> 
    <script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="js/lightbox.js"></script>
    <script type="text/javascript">
        $(function(){
            var LightBox = new LightBox();
        });
    </script>
</body>
</html>   

正常应该alert弹出框吧,但控制台如下错误是为什么?


  1. Window => window

  2. 既然你要把LightBox当作是全局的类,那为啥不直接写在外面,反而要用现在这么别扭的方式去写……


这个地方这样对吗
LightBox.prototype = {

};

命名不要一样。。


Window 改成 window

【热门文章】
【热门文章】