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')
我想传递一个函数名(update1或update2或update3)来更新函数
输出应为:updated tyres with update1;
答案 0 :(得分:3)
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[updateFn]( s)