简体   繁体   中英

call anonymous function javascript

I need to call a function passing argument. Here is my code.

 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(); 

What I really need is when I click 'Like' button, I need to console the url ie 'facebook.com' that I pass.

You don't need to use call ( this.doLike.call(...) ) to call a function, you can just do 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(); 

This is not the best way to do this. You should create links using document.createElement , attach event listeners to them EventTarget#addEventListener and then append those links to DOM using Node#appendChild

 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(); 

I agree with @ponury-kostek 's answer, plus in the following line of code

myfb = new MyFaceBook(response = array());

you could use

myfb = new MyFaceBook("your response here");

in case of an array :

myfb = new MyFaceBook(["your response here","another response..."]);

here is a full executable code that works :

 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(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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