admin管理员组文章数量:1244269
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.
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.
Share Improve this question edited Dec 27, 2009 at 20:02 Gumbo 655k112 gold badges791 silver badges851 bronze badges asked Dec 27, 2009 at 19:47 ZibriZibri 9,8473 gold badges60 silver badges48 bronze badges 3- Why do you need that information? – Gumbo Commented Dec 27, 2009 at 20:08
- 1 You're doing it wrong. The name of the variable you're assigning the array to should be pletely and utterly irrelevant. If it isn't, you should re-factor your code. – August Lilleaas Commented Dec 29, 2009 at 11:58
- What a useless ment. Just say you didn't read all the discussion and you didn't understand the question. – Zibri Commented Jan 4, 2010 at 19:03
6 Answers
Reset to default 5The best you can do is to always explicitly set the array's name when you create it:
var z = [];
z.name = 'z';
You could do this by having a function makeArray that sets the name passed as an argument:
function makeArray(name) {
var arr = [];
arr.name = name;
return arr;
}
The essential problem is that the several variables can point to the same array:
var z = [],
x = z;
Then what should the name be: z or x?
The problem is that a variable (like an array) can have several names. For example:
var a = new Array();
var b = a;
a[0] = "hello";
alert(b[0]);//"hello"
What is the name of the array, a or b?
Can't be done. There is no way to access the name of the variable which is storing a reference to the object. Perhaps you should explain why you need behavior like this, so someone can suggest you an alternative way to approach the problem.
The only way to do this would be to brute-force check all properties of the global object (assuming the variable is global) until you find a property that === the array. Of course, it could be referenced by multiple variables so you would have to pick one of the names you get. This implementation gets the first variable to reference the array and will work in browsers and web worker threads:
Array.prototype.getName = function () {
var prop;
for (prop in self) {
if (Object.prototype.hasOwnProperty.call(self, prop) && self[prop] === this) {
return prop;
}
}
return ""; // no name found
};
Of course, I don't remend this at all. Do not do this.
Further testing the object.getName().. i found this 'problem':
test1='test'
test
test2='test'
test
test1.getName()
test1
test2.getName()
test1
this happens because they have the same content and getName checks for content. a solution could be to return an Array in that case.
But I'm still searching for a definitive solution to this brainteasing problem.
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.
本文标签: javascriptObject (string or array) NAME how to get itStack Overflow
版权声明:本文标题:javascript - Object (string or array) NAME. how to get it? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740199851a2240006.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论