0

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 i.e 'facebook.com' that I pass.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

yes, that's does the work. But the function is executed automatically without click event. How can I prevent it from running automatically.
@user2542724 Is this what you expected?
Actually, when I click on "Like" link, I need to console the "facebook.com". But right now, the function doLike is called automatically when the script is loaded.
@user2542724 have you clicked Like link?
0

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

1 Comment

yes, the above code works fine now. But the function 'doLike' is executed automatically without click event. How can I prevent it from running automatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.