我需要调用一个函数传递参数。 这是我的代码。
var myfb;
var MyFaceBook = function(response) {
this.response = response;
};
MyFaceBook.prototype.dd = function(_array) {
console.log(_array);
}
MyFaceBook.prototype.doLike = function(_url) {
this.dd(_url);
return false;
}
MyFaceBook.prototype.getTabList = function() {
return '<a href="#" onclick="'+this.doLike.call('http://facebook.com')+'"> Like </a> | <a href="#">Share</a> | <a href="#"> Comment </a>';
};
myfb = new MyFaceBook(response = array());
myfb.getTabList();
我真正需要的是,当我单击“赞”按钮时,我需要管理所传递的URL,即“ facebook.com”。
答案 0 :(得分:1)
您无需使用call
(this.doLike.call(...)
)来调用函数,只需执行this.doLike(...)
var myfb;
var MyFaceBook = function(response) {
this.response = response;
};
MyFaceBook.prototype.dd = function(_array) {
console.log(_array);
}
MyFaceBook.prototype.doLike = function(_url) {
this.dd(_url);
return false;
}
MyFaceBook.prototype.getTabList = function() {
return '<a href="#" onclick="myfb.doLike(\'http://facebook.com\')"> Like </a> | <a href="#">Share</a> | <a href="#"> Comment </a>';
};
myfb = new MyFaceBook([]);
document.body.innerHTML += myfb.getTabList();
这不是执行此操作的最佳方法。您应该使用document.createElement
创建链接,将事件监听器附加到EventTarget#addEventListener
,然后使用Node#appendChild
DOM
const MyFaceBook = function(response) {
this.response = response;
};
MyFaceBook.prototype.dd = function(_array) {
console.log(_array);
}
MyFaceBook.prototype.doLike = function(_url) {
this.dd(_url);
return false;
}
MyFaceBook.prototype.getTabList = function() {
const link = document.createElement('a');
link.innerHTML = 'Like';
link.addEventListener("click", () => {
this.doLike('http://facebook.com');
});
document.body.appendChild(link);
};
const myfb = new MyFaceBook([]);
myfb.getTabList();
答案 1 :(得分:0)
我同意@ ponury-kostek的回答,以及以下代码行
myfb = new MyFaceBook(response = array());
您可以使用
myfb = new MyFaceBook("your response here");
如果是数组:
myfb = new MyFaceBook(["your response here","another response..."]);
这是有效的完整可执行代码:
var myfb;
var MyFaceBook = function(response) {
this.response = response;
};
MyFaceBook.prototype.dd = function(_array) {
console.log(_array);
}
MyFaceBook.prototype.doLike = function(_url) {
this.dd(_url);
return false;
}
MyFaceBook.prototype.getTabList = function() {
return '<a href="#" onclick="'+this.doLike('http://facebook.com')+'"> Like </a> | <a href="#">Share</a> | <a href="#"> Comment </a>';
};
myfb = new MyFaceBook(["my response"]);
myfb.getTabList();