Reputation: 31
Is it possible to get string "markHotel" in this code?
this.markHotel = this.markPrice = function() {
// get "markHotel"
};
this.markHotel();
Upvotes: 3
Views: 109
Reputation: 1359
You could use Function.prototype.bind(). Here's a simple example:
function base() {
console.log(this.name);
// do some other stuff using this.name or other this props...
}
var markPrice = base.bind({ name: 'markPrice' });
var markHotel = base.bind({ name: 'markHotel' });
// this will log 'markPrice'
markPrice();
// this will log 'markHotel'
markHotel();
It looks like you may be doing this inside a class constructor, but it's not totally clear from your example. If that's the case, make sure not to confuse the class constructor's "this" context and the "base" function's "this" context, the latter being manually bound when setting markPrice and markHotel.
Docs for bind: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
Here's a pen: http://codepen.io/bsidelinger912/pen/QKLvzL
Upvotes: 1