Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript未定义_Javascript - Fatal编程技术网

javascript未定义

javascript未定义,javascript,Javascript,为什么当ajax成功返回时我不能访问渲染函数?也许我疯了,但我以前做过 它告诉我this.render不是一个函数 DataItem.prototype = { display: function () { $('body').append(this.name + ": " + this.getData(this.rootData, this.subData) + "<br />"); }, getData: function (rootDat

为什么当ajax成功返回时我不能访问渲染函数?也许我疯了,但我以前做过

它告诉我this.render不是一个函数

DataItem.prototype = {
    display: function () {
        $('body').append(this.name + ": " + this.getData(this.rootData, this.subData) + "<br />");
    },
    getData: function (rootData, subData) {
        $.ajax({
            type: "GET", 
            url: "json/data.js",
            data: "",
            dataType: "json",
            success: function (json){
                this.render(json);
            }
        });
    },
    render: function (json) {
        var res = [];
        for(var i=0, t; t=json.log.entries[i]; i++) {
            var p = t.request.url;
            if (p!=undefined) res.push(p);
        }
        return res.length;
    }
};
DataItem.prototype={
显示:函数(){
$('body').append(this.name+“:“+this.getData(this.rootData,this.subData)+”
); }, getData:函数(rootData、子数据){ $.ajax({ 键入:“获取”, url:“json/data.js”, 数据:“, 数据类型:“json”, 成功:函数(json){ this.render(json); } }); }, 呈现:函数(json){ var-res=[]; for(var i=0,t;t=json.log.entries[i];i++){ var p=t.request.url; 如果(p!=未定义)res.push(p); } 返回res.length; } };
尝试调用
this.render()时,作用域已更改。
。我相信
这个
包含ajax请求对象,而不是
数据项
对象

一个简单的解决方案是这样做:

getData: function (rootData, subData) {
    var self = this;
    $.ajax({
        type: "GET", 
        url: "json/data.js",
        data: "",
        dataType: "json",
        success: function (json){
            self.render(json);
        }
    });
},

编辑:我错了,在success函数中,
这个
变量包含ajax请求的选项,但是我的解决方案仍然正确。请参阅jQuery文档()中的更多内容。

当您尝试调用
this.render()时,作用域已更改。
。我相信
这个
包含ajax请求对象,而不是
数据项
对象

一个简单的解决方案是这样做:

getData: function (rootData, subData) {
    var self = this;
    $.ajax({
        type: "GET", 
        url: "json/data.js",
        data: "",
        dataType: "json",
        success: function (json){
            self.render(json);
        }
    });
},
编辑:我错了,在success函数中,
这个
变量包含ajax请求的选项,但是我的解决方案仍然正确。请参阅jQuery文档()

中的更多内容,以便添加到答案中。如果要将
success
函数外部化,而不是使用匿名函数,可以使用以下方法传递其他参数:

function handleSuccess(json) {
   this.self.render(json);
}

$(function() {
    $.ajax({
        type: "GET", 
        url: "json/data.js",
        data: "",
        dataType: "json",
        // pass an additional parameter to the success callback
        self: this,
        success: handleSuccess
    });
});
只是补充一下答案。如果要将
success
函数外部化,而不是使用匿名函数,可以使用以下方法传递其他参数:

function handleSuccess(json) {
   this.self.render(json);
}

$(function() {
    $.ajax({
        type: "GET", 
        url: "json/data.js",
        data: "",
        dataType: "json",
        // pass an additional parameter to the success callback
        self: this,
        success: handleSuccess
    });
});

由于以下代码有效(即打印了“abcd”),我不确定您面临的问题是什么,除非您愿意共享更多代码

<script>
    DataItem = function(){};
    DataItem.prototype = {
        display: function () {
            return 'a';
        },
        getData: function () {
            document.write(this.render());
            return this;
        },
        render: function () { return "abcd"; }
    };


    di = new DataItem();
    di.getData();

</script>

DataItem=函数(){};
DataItem.prototype={
显示:函数(){
返回“a”;
},
getData:函数(){
document.write(this.render());
归还这个;
},
render:function(){return“abcd”;}
};
di=新数据项();
di.getData();

由于以下代码有效(即“abcd”已打印),我不确定您面临的问题是什么,除非您愿意共享更多代码

<script>
    DataItem = function(){};
    DataItem.prototype = {
        display: function () {
            return 'a';
        },
        getData: function () {
            document.write(this.render());
            return this;
        },
        render: function () { return "abcd"; }
    };


    di = new DataItem();
    di.getData();

</script>

DataItem=函数(){};
DataItem.prototype={
显示:函数(){
返回“a”;
},
getData:函数(){
document.write(this.render());
归还这个;
},
render:function(){return“abcd”;}
};
di=新数据项();
di.getData();

您如何使用该函数?@Alan-您能显示DataItem的完整代码吗?您如何使用该函数?@Alan-您能显示DataItem的完整代码吗?