如果我在构造函数中创建内部类,它是否为我创建的外部类的每个实例分配了内存?例如,
class PriorityQueue {
constructor(maxSize) {
// Set default max size if not provided
if (isNaN(maxSize)) {
maxSize = 10;
}
this.maxSize = maxSize;
// Init an array that'll contain the queue values.
this.container = [];
// Create an inner class that we'll use to create new nodes in the queue
// Each element has some data and a priority
this.Element = class {
constructor(data, priority) {
this.data = data;
this.priority = priority;
}
};
}
}
是否为Priority Queue类的每个实例创建了Element类?有没有办法使这个类静态?
答案 0 :(得分:4)
因为您在构造函数中创建并将类分配给this.Element
,并且每次PriorityQueue
实例化时构造函数都会运行 - 是的,您正在创建新的Class
es对于每个实例。如果您只需要一个Class用于所有实例化,请将其放在原型上:
class PriorityQueue {
constructor(maxSize) {
// Set default max size if not provided
if (isNaN(maxSize)) {
maxSize = 10;
}
this.maxSize = maxSize;
// Init an array that'll contain the queue values.
this.container = [];
}
}
PriorityQueue.prototype.Element = class {
constructor(data, priority) {
this.data = data;
this.priority = priority;
}
}
const p1 = new PriorityQueue(1);
const p2 = new PriorityQueue(2);
console.log(p1.Element === p2.Element);

答案 1 :(得分:1)
是否为Priority Queue类的每个实例创建了Element类?
是的,有点儿。它不会创建一个全新的类,只是类构造函数的闭包。对内存的影响非常小,但在创建几千个优先级队列时可能会很明显。
答案 2 :(得分:1)
构造函数原型的方法具有通过this
关键字访问实例化对象的权限。但是,一旦完成,您就无法使用此权限。根据您的问题猜测,您可能需要以下功能;
class PriorityQueue {
constructor(maxSize) {
this.maxSize = maxSize || 10;
// Init an array that'll contain the queue values.
this.container = [];
}
// Create an inner class that we'll use to create new nodes in the queue
// Each element has some data and a priority
enqueueNode(data,priority){
class Element {
constructor(data, priority) {
this.data = data;
this.priority = priority;
}
};
return this.container.length < this.maxSize ? (this.container.push(new Element(data,priority)),
this.container.sort((a,b) => a.priority - b.priority),
true)
: false;
}
}
var p1 = new PriorityQueue();
p1.enqueueNode("test",3);
p1.enqueueNode("best",1)
console.log(p1.container);
&#13;
.sort()
部分也许可以更有效地实施,但为了保持在我使用.sort()
的问题的上下文中。此代码也没有效率,因为我们还为实例化的每个节点反复定义了Element类。正如我在评论中提到的那样,最好单独定义Element类。
class Element {
constructor(data, priority){
this.data = data;
this.priority = priority;
}
}
class PriorityQueue {
constructor(maxSize) {
this.maxSize = maxSize || 10;
// Init an array that'll contain the queue values.
this.container = [];
}
// Create an inner class that we'll use to create new nodes in the queue
// Each element has some data and a priority
enqueueNode(data,priority){
return this.container.length < this.maxSize ? (this.container.push(new Element(data,priority)),
this.container.sort((a,b) => a.priority - b.priority),
true)
: false;
}
}
var p1 = new PriorityQueue();
p1.enqueueNode("test",3);
p1.enqueueNode("best",1)
console.log(p1.container);
&#13;
好的......只有Element
实例的对象才需要PriorityQueue
类,所以你不希望它像流浪狗一样徘徊。好的,我认为最好使它成为PriorityQueue
的静态函数。然后我们像new PriorityQueue.Element(data,priority)
一样调用它,这也是非常具有描述性的。
class PriorityQueue {
constructor(maxSize) {
this.maxSize = maxSize || 10;
// Init an array that'll contain the queue values.
this.container = [];
}
// Create an inner class that we'll use to create new nodes in the queue
// Each element has some data and a priority
enqueueNode(data,priority){
return this.container.length < this.maxSize ? (this.container.push(new PriorityQueue.Element(data,priority)),
this.container.sort((a,b) => a.priority - b.priority),
true)
: false;
}
}
PriorityQueue.Element = class Element {
constructor(data, priority){
this.data = data;
this.priority = priority;
}
};
var p1 = new PriorityQueue();
p1.enqueueNode("test",3);
p1.enqueueNode("best",1)
console.log(p1.container);
&#13;