【发布时间】:2014-06-02 14:29:53
【问题描述】:
我已经构建了一个 Chrome 扩展来执行跨域 ajax 请求... 现在我试图将它包装到一个 javascript 面向对象的框架中。代码如下:
function webpage(url)
{
this.url = url;
this.html = "";
}
webpage.prototype.set_html = function(html)
{
console.log(html); // working
this.html = html; // not working
}
webpage.prototype.get_html = function()
{
request_url(this.url, this.set_html); // call to a working chrome extension
}
var mypage = new webpage("http://www.google.it");
mypage.get_html();
问题是原型函数“set_html”如果我称它为“正常方式”,效果很好
mypage.set_html("hello world")
“request_url”函数也很好用
request_url("http://www.google.it",function(data){console.log(data)})
但是当我这样称呼它时
mypage.get_html();
第一个“console.log”效果很好,它显示了 page_source,但 this.html 的值不会改变......我错在哪里?
【问题讨论】:
标签: javascript google-chrome object callback this