3

Is it possible to get string "markHotel" in this code?

this.markHotel = this.markPrice = function() {

    // get "markHotel"

};

this.markHotel();
holden321
  • 31
  • 2
  • http://stackoverflow.com/questions/2648293/javascript-get-function-name – Rob M. Aug 30 '16 at 16:25
  • @RobM. The issue here is that the function itself is anonymous and has no name. I'm not sure you can do what the OP is asking here. – James Thorpe Aug 30 '16 at 16:26
  • I think the only way you could really do this would be to throw an exception, catch it, and parse the stack which is platform specific and is not obligated to include it. So not really, but well-formed code should not need to anyway. – Alexander O'Mara Aug 30 '16 at 16:28
  • 2
    If you _really_ need to do this (it looks a bit smelly - is this an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)?), you could make `markHotel` and `markPrice` independent functions that call into your anonymous function, passing in the name. – James Thorpe Aug 30 '16 at 16:30
  • I think, the answer is "no", unless you have additional control over object. – Qwertiy Aug 30 '16 at 16:30
  • I agree with @JamesThorpe - seems like an XY problem. I can't think of a good reason you'd need to do that sort of thing. – VLAZ Aug 30 '16 at 16:31
  • Iside this function I want to place some common code for all functions (markHotel, markPrice and so on) The call of this.markHotel come from outer code. In real code this is someObj.markHotel(); – holden321 Aug 30 '16 at 16:35
  • 1
    @holden321 then it is an XY problem. You don't want to get _what was called_ you just need common code. You can have something like `function common() { /* common code */ }` and then something like (effectively) `this.markHotel = function() { common(); /* more code*/ }`. This can also be done with `common` accepting callbacks or functional composition or few other ways. Whatever the case, making the function changed based on how you called it is a code smell. – VLAZ Aug 30 '16 at 16:37
  • This is what I am trying to avoid, repeating common() in each function. I want something like functionname(); inside common code. – holden321 Aug 30 '16 at 16:45
  • 1
    `function makeMarkFunction(callback) { return function() {/* common code */; callback()} }` and then `this.markHotel = makeMarkFunction(function() {/* markHotel specific stuff */})`. It keeps your things nice and generic, you don't need to care what calls your function. You _shouldn't_ need to care what calls it. – VLAZ Aug 30 '16 at 16:50

1 Answers1

1

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

Ben Sidelinger
  • 1,309
  • 7
  • 12