简体   繁体   English

调用javascript原型方法时未定义的函数

[英]undefined function when calling javascript prototype method

i have the following javascript object: 我有以下javascript对象:

var Playlist = function(callback) {
    $.ajax({
        url:'/playlist'
    })
    .done(function(data) {
        this.html = data;
        callback({},this);
    })
    .fail(function(err) {
        callback(err,{});
    });
};

for this object, i declared a prototype method: 对于这个对象,我声明了一个原型方法:

Playlist.prototype = {
    render : function() {
        $('#main').html(this.html);
    }
};

now, when i create a new object und try to call the 'render' function like this 现在,当我创建一个新对象并尝试像这样调用“ render”功能时

function renderPlaylist() {
    var playlist = new Playlist(function(err, obj) {
        obj.render();
    });
}

renderPlaylist();

i get a 'undefined is not a function'. 我得到一个“未定义不是函数”。 it seems not to know the render function, which i declared in the prototype. 似乎不知道我在原型中声明的render函数。 what am i missing here? 我在这里想念什么? thanks for help. 感谢帮助。

with the help of Volune , i changed my constructor to Volune的帮助下,我将构造函数更改为

var Playlist = function(callback) {
    var playlist = this;
    $.ajax({
        url:'/mpdplaylist'
    })
    .done(function(data) {
        playlist.html = data;
        callback(null,playlist);
    })
    .fail(function(err) {
        callback(err,{});
    });
};

now it works. 现在可以了。 thank you!! 谢谢!!

edit: as Volune pointed out, i replaced the empty object for the error with null in the 'done' callback function 编辑:正如Volune所指出的,我在'done'回调函数中用null替换了错误的空对象

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

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