Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ES6-从类内获取实例名称_Javascript_Node.js_Es6 Class - Fatal编程技术网

Javascript ES6-从类内获取实例名称

Javascript ES6-从类内获取实例名称,javascript,node.js,es6-class,Javascript,Node.js,Es6 Class,我希望能够从类中获取实例化类的实例的名称 例如: class MyClass { getInstanceName() { // code that figures out the name given to the instance // instantiating this class } } var inst = new MyClass(); console.log(inst.getInstanceName()); // should lo

我希望能够从类中获取实例化类的实例的名称

例如:

class MyClass {
    getInstanceName() {
        // code that figures out the name given to the instance 
        // instantiating this class
    }
}

var inst = new MyClass();
console.log(inst.getInstanceName()); // should log "inst"
我想让它从类中发出一个实例名为的事件。我在node.js中做这件事

我希望能够从类中获取实例分配给的变量的名称

这在Javascript中是不可能的

一个对象可以分配给数百个变量。只有语言实现的内部(如垃圾收集器使用的信息)知道哪些变量包含对给定对象的引用。这不是提供给该语言用户的信息

如果在您的环境中,您希望一个对象存储这样的信息,那么您必须自己将名称传递给构造函数。然后,对象的构造函数可以保存该字符串,以便稍后调用
getInstanceName()
方法时提供。没有办法自动做到这一点

下面是存储名称的构造函数的一个简单实现

var inst = new MyClass("inst");

class MyClass {
    constructor(name) {
        this._name = name;
    }
    getInstanceName() {
        return this._name;
    }
}

或者,不可设置、不可枚举的属性:

class MyClass {
    constructor(name) {
        Object.defineProperty(this, "_name", {
            configurable: false,
            enumerable: false,
            value: name,
            writable: false
        });
    }
    getInstanceName() {
        return this._name;
    }
}
可以简化为:

class MyClass {
    constructor(name) {
        Object.defineProperty(this, "_name", {value: name});
    }
    getInstanceName() {
        return this._name;
    }
}
因为使用
对象时,
可配置
可枚举
可写
都默认为
false


或者,如果不想在对象上使用可访问属性,可以通过以下操作将其设置为私有:

const MyClass = (function() {
    // keep track of names in a private space
    const names = new WeakMap();


    class MyClass {
        constructor(name) {
            names.set(this, name);
        }
        getInstanceName() {
            return names.get(this);
        }
    }
    return MyClass;
})();

var inst = new MyClass("inst");
作为包含相同引用的多个变量的示例,请看以下内容:

var x, y, z;
x = y = z = new MyClass();
现在,您有三个独立的变量,它们都引用同一个对象。因此,即使该语言想要执行您要求的操作(它没有),也没有包含对给定对象引用的规范变量

或者,这是:

function doSomething(obj) {
   var t = obj;
   // at this point, there are 5 separate variables with a reference to your object
}

var x, y, z;
x = y = z = new MyClass();

doSomething(x);

对同一对象有5个单独的引用。没有规范变量。

“inst”是变量名,而不是类型。如果创建一个实例
var x=new x()
,然后将该实例分配给一个新变量
var y=x
,getInstanceName会做什么?x和y都指向同一个对象如果匿名使用该对象会怎么样<代码>新建MyClass()。getInstanceName()不是“实例名称”。它是一个变量,实例恰好已分配给该变量。