javascript javascript获取父对象/变量的名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10838032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:12:13  来源:igfitidea点击:

javascript get name of parent object/variable

javascriptobject

提问by slinkhi

Lets say I have

可以说我有

var myObject = {

  'myFunction' : function () {
    // something here that lets me get 'myObject'
  }

}

I have tried various functions found here and stuff like this.constructor.namebut i always get 'Object' as the value returned. Is there any way to get the actual name of the variable in this situation?

我尝试了在这里找到的各种函数和类似的东西,this.constructor.name但我总是得到“对象”作为返回的值。在这种情况下,有没有办法获得变量的实际名称?

edit to explain the whyso maybe people will better understand...I want to be able to make a function that is continuously called with setInterval. Something like this:

编辑解释为什么这样也许人们会更好地理解......我希望能够制作一个使用 setInterval 连续调用的函数。像这样的东西:

var myObject = {
  'intval' : '',
  'timeout' : 500,

  'start' : function () {
    this.objectName = 'myObject'; // <--I want this to be dynamically popped
    this.intval=window.setInterval("window['"+this.objectName+"'].myFunction()",this.timeout);
  },

  'stop' : function () {
    window.clearInterval(this.intval);
    this.intval='';
  },

  'myFunction' : function () {
    // do something
  }
}

This works just fine if i hardcode 'myObject' to this.objectName but I don't want it to be hardcoded. Problem is that I wan't just do setInterval("window[this.objectName]",100) becausethisis not in the right context whensetInterval` is run, and I don't want to have the objectname hardcoded

如果我将 'myObject' 硬编码到 this.objectName 但我不希望它被硬编码,这很好用。问题是,我wan't只是做setInterval("window[this.objectName]",100) because这个is not in the right context whensetInterval`运行,我不希望有对象名硬编码

采纳答案by Alex K.

One way;

单程;

var myObject = {
    'myFunction' : function () {
        for (var name in window) {
            if (window[name] === this) { 
                alert(name);
                break;
            }
        }
     }
}

Not very nice IMO, marginally better if you wrap it in your own namespace.

不是很好的 IMO,如果你把它包装在你自己的命名空间中会稍微好一点。

You could also always

你也可以一直

var myObject = {
    moniker: "myObject",
    ...

In light of your update you don't need to resolve the object at all;

根据您的更新,您根本不需要解析对象;

var myObject = {
    'intval' : '',
    'timeout' : 1500,
    'start' : function () {
        var self = this;
        this.intval = window.setInterval(function() {
            self.myFunction()
        }, this.timeout);
    },
    'myFunction' : function() {
        alert(this.intval);
    }
}

回答by Mathletics

No, you can't do that in JavaScript.

不,你不能在 JavaScript 中做到这一点。

回答by Matt McMahon

In short, nope.

简而言之,没有。

The way you're creating your objects, myObjectis nothing more than a label with a (relatively loose) affiliation with an object literal. In Java or C++ terms, myObjectis more like a pointer then a class name. If you want a stronger link between the label myObjectand the object it references, you'll need to use a constructor or subclass pattern. See this articleon golimojo.com for a pretty solid overview.

您创建对象的myObject方式无非是与对象文字具有(相对松散的)从属关系的标签。在 Java 或 C++ 术语中,myObject更像是一个指针,而不是一个类名。如果您希望标签myObject与其引用的对象之间建立更牢固的联系,则需要使用构造函数或子类模式。请参阅golimojo.com 上的这篇文章以获得非常可靠的概述。

回答by jbabey