Javascript ES6 - 从 class 中获取实例名称
Javascript ES6 - get name of instance from within class
我希望能够从 class.
中获取实例化 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 log "inst"
我想让它从 class 中使用实例名称发出一个事件。我在 node.js
中这样做
I'd like to be able to get the name of the variable the instance was assigned to, from within the class.
这在 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;
}
}
因为 configurable
、enumerable
和 writable
在您使用 Object.defineProperty()
.
时都默认为 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 个单独的引用。没有规范变量。
我希望能够从 class.
中获取实例化 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 log "inst"
我想让它从 class 中使用实例名称发出一个事件。我在 node.js
中这样做I'd like to be able to get the name of the variable the instance was assigned to, from within the class.
这在 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;
}
}
因为 configurable
、enumerable
和 writable
在您使用 Object.defineProperty()
.
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 个单独的引用。没有规范变量。