繁体   English   中英

未捕获的ReferenceError:“方法”未定义

[英]Uncaught ReferenceError: “method” is not defined

我创建了一个javascript对象

var Article = function(data) {
    this.foo = data,
    this.get_more_data = function() {
        // do something, get a response
        show_data(response);
    },
    this.show_data = function(bar) {
        //do something with bar;
    }
};

如果不使用此方法编写show_data方法,则效果很好。 但是在对象外部无法访问。 有了这个。 我从Chrome控制台收到“ Uncaught ReferenceError”。

为什么是这样?

谢谢。

你应该叫show_data为的方法this ,不是一个功能限制在当前的背景下:

var Article = function(data) {
    this.foo = data,
    this.get_more_data = function() {
        // do something, get a response
        this.show_data(this.foo);
    },
    this.show_data = function(bar) {
        console.log(bar);
    }
};

暂无
暂无

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

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