0

How can i get a function name without knowing it in advance.

db = {
    base: {
        getById: function() {
            console.log(methodName);
        }
    }
};

I want to call db.base.getById() and console to log 'base'; Please give me a hand :)

Aleksandrenko
  • 2,987
  • 6
  • 28
  • 34
  • 2
    You can't. The function or object could be assigned to multiple properties / variables. It is not a unidirectional relationship. – Felix Kling May 31 '13 at 20:03

1 Answers1

2

You can't do that.

What you can do is to use a different pattern, for example a factory :

db = {
    base: (function(){
        var b = {};
        b.getById: function() {
            console.log(b);
        }
        return b;
    })()
};
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • I'll probably delete this answer, I'm not sure it's helpful here. – Denys Séguret May 31 '13 at 20:06
  • So ... i can't do as i like it, so i will do it with a factory :) – Aleksandrenko May 31 '13 at 20:18
  • Did you notice this logs the whole base object and not the "base" string ? I'm not sure of what you want or need. – Denys Séguret May 31 '13 at 20:20
  • mhm, but i needed the 'base' string. The idea is to make a 'base' model with methods. And something like this: db.base -> basic methods like create, getById, getAll and then db.person = base so i can call db.person... and there are the same methods but for 'person' perspective. So i don't duplicate code ... – Aleksandrenko May 31 '13 at 20:30
  • Your problem is not very clear to me but don't you need prototype based classes? – Denys Séguret May 31 '13 at 20:35