0
..
Class.prototype.property = function(){
return(this.prototypeobject.name);
}
..

oClass = new Class();
alert(oClass.property());

It's simple(or maybe not?). I just want to get the current prototype object name as String.
Note: this.prototypeobject.name doensn't work. It's just an example.

Sylnois
  • 1,589
  • 6
  • 23
  • 47
  • `return` isn't a function, `return this.prototypeobject.name;` is equivalent. – RobG Oct 04 '13 at 11:46
  • @RobG I don't get it. I know, that return isn't a function. I just want to return the Prototype object name as String(in my case "property"). – Sylnois Oct 04 '13 at 11:51
  • What do you `.prototypeobject` expect to be? I've never heard of such property in [`Class`](http://api.prototypejs.org/language/Class/). – Bergi Oct 04 '13 at 11:55
  • Can you show an example where you would need this? – Bergi Oct 04 '13 at 12:04
  • @Bergi I want to call a function in the property function. The called function need to know from which property function it was called. Sure `callFunction('property')` in `Class.prototype.property` would do the trick, but i don't want to use String values, cause the posted parameter is always like the prototype object function. – Sylnois Oct 04 '13 at 12:11
  • You cannot. And what exactly does the `callFunction` need the caller's method name for? – Bergi Oct 04 '13 at 12:20
  • The `callFunction` includes a ajax call to a php script. The php script needs to know, which function he should call. – Sylnois Oct 04 '13 at 12:38
  • How does the PHP script call a javascript function? If you are using JSONP, then you just should pass the callback's name as a literal. Or maybe create that dynamically and use a reference to the actual method. – Bergi Oct 05 '13 at 17:14

1 Answers1

0

There is no such reflection functionality. A function doesn't know itself other than by explicit reference (arguments.callee is deprecated), and function objects are not bound to any properties anyway so they cannot know the respective property name. Whenever you need a "method name", hardcode it as a string literal.


OK, there is something you could do (using a named function expression, you can change it to a function declaration for IE):

Constr.prototype.someProperty = function myFuncName(args…) {
    var propertyName = "";
    for (var p in this)
        if (this[p] == myFuncName) {
            propertyName = p;
            break;
        }
    alert("this function (myFuncName) was found on property '"
         +propertyName+"' of `this` object");
};

var inst = new Constr(…);
inst.someProperty(…); // alerts "… found on property 'someProperty' …"

However, this is an ugly hack and you should not use it.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375