Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
134
rated 0 times [  137] [ 3]  / answers: 1 / hits: 21043  / 15 Years ago, sun, december 27, 2009, 12:00:00

I need a prototype done in this way:



Array.prototype.getname=function(){ [...]return arrayname; }


So I can:



z=new Array;
alert(z.name);


and I should have z in the alert.



I'm working on Chrome and caller/callee seem to return empty.


More From » arrays

 Answers
3

So For now the best answer is Elijah's



More generally:



Object.prototype.getName = function () { 
var prop;
for (prop in self) {
if (Object.prototype.hasOwnProperty.call(self, prop) && self[prop] === this && self[prop].constructor == this.constructor) {
return prop;
}
}
return ; // no name found
};


I wonder if there are better solutions.


[#97978] Wednesday, December 23, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rossthomasn

Total Points: 122
Total Questions: 78
Total Answers: 105

Location: South Georgia
Member since Sun, Aug 8, 2021
3 Years ago
;