简体   繁体   English

对象(字符串或数组)NAME。 如何获得?

[英]Object (string or array) NAME. how to get it?

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. 我应该在警报中输入“ z”

I'm working on Chrome and caller/callee seem to return empty. 我正在使用Chrome,而呼叫者/被呼叫者似乎返回了空白。

The 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: 您可以通过使用makeArray函数来设置作为参数传递的名称来做到这一点:

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? 那么名称应该是:z或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? 数组的名称是a还是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: 此实现获取第一个引用该数组的变量,并将在浏览器和Web Worker线程中工作:

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 recommend this at all. 当然,我根本不建议这样做。 Do not do this. 不要这样做。

Further testing the object.getName().. i found this 'problem': 进一步测试object.getName()..我发现了这个“问题”:

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. 发生这种情况是因为它们具有相同的内容,并且getName检查内容。 a solution could be to return an Array in that case. 一种解决方案是在这种情况下返回Array。

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. 我想知道是否有更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM