简体   繁体   English

IE8中未定义类

[英]Class is undefined in IE8

I have stupidly decided to support IE8 in my latest project, something which will no doubt go down in history as the dumbest idea of my life. 我愚蠢地决定在我的最新项目中支持IE8,这无疑会成为我生命中最愚蠢的想法。

So the most fundamental problem I'm running into is that my main class variable is undefined. 所以我遇到的最基本的问题是我的主类变量是未定义的。 What I mean is I have a prototype set up in a file general.js that looks a bit like this: 我的意思是我在一个文件general.js中设置了一个原型,看起来有点像这样:

var generalClass;

// jQuery Object
var $ = jQuery; 

$(document).ready(function() {

    // A general class for a general file.
    generalClass = function() {

    }

    generalClass.prototype = {

    }


    new generalClass();     


});

So the generalClass variable is filled up with my prototype/etc. 所以generalClass变量用我的原型/等填充。 I then include this in the head of my document and later on I call upon a function in that generalClass for something else, a bit like this: 然后我把它包含在我的文档的头部,稍后我调用了generalClass中的一个函数来做其他事情,有点像这样:

<script type="text/javascript" src="general.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $.ajax({
        type: 'POST', 
        url: ..., 
        data: {

        }, 
        success : function(data) {
            // CALL MY FUNCTION:    
            generalClass.prototype.myFunction();

        }


    }
});
</script>

In every browser, from IE9 to Chrome this works. 在每个浏览器中,从IE9到Chrome都可以。 In IE8 this does not work, and generalClass is undefined. 在IE8中,这不起作用,并且generalClass未定义。 Why is it doing this to me? 为什么这样对我?

I am not sure where you learned that pattern, but it should be more like this: 我不确定你在哪里学习这种模式,但它应该更像这样:

var generalClass;

// jQuery Object
//var $ = jQuery;  <-- makes no sense $ should be jQuery already

$(document).ready(function() {

    function GeneralClass() {}
    GeneralClass.prototype = {
        myFunction: function () {
            alert("x");
        }
    };

    generalClass = new GeneralClass();

});

and when you call it 当你打电话给它

generalClass.myFunction();

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

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