3
function car() {
}

car.prototype = {
 update1: function(s) {
   console.log("updated "+s+" with update1")
 },
 update2: function(s) {
   console.log("updated "+s+" with update2")
 },
 update3: function(s) {
   console.log("updated "+s+" with update3")
 },
 update: function(s, updateFn) {
   this.updateFn.call(this, s)
 }
}

var c = new car()

c.update("tyres", 'update1')

I want to pass a function name (update1 or update2 or update3) to update function

output should be : updated tyres with update1;

1 Answers1

3

http://jsfiddle.net/x8jwavje/

 function car() {
    }

car.prototype = {

 update1: function(s) {
   console.log("updated "+s+" with update1")
 },
 update2: function(s) {
   console.log("updated "+s+" with update1")
 },
 update3: function(s) {
   console.log("updated "+s+" with update1")
 },
 update: function(s, updateFn) {

   this[updateFn]( s)
 }
}

var c = new car()

c.update("tyres", 'update1')

this is how you should call a function whose name passed as a parameter this[updateFn]( s)

Edit: http://jsfiddle.net/x8jwavje/1/

Naeem Shaikh
  • 15,331
  • 6
  • 50
  • 88