Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a prototype function name to another prototype function [duplicate]

Tags:

javascript

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;

like image 367
Malleswar Chinta Avatar asked Sep 03 '26 11:09

Malleswar Chinta


1 Answers

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/

like image 115
Naeem Shaikh Avatar answered Sep 05 '26 00:09

Naeem Shaikh