使用生成器函数作为 WeakMap 的值时出错
Error using a generator function as value of a WeakMap
我正在自己构建一个链表。我试图在构造函数中将生成器分配为 key/value WeakMap 的值。 _iterator
是一个 WeakMap,因为它是我想用来对我的数据结构进行简单迭代的私有成员,但我不想让生成器在外部可见,否则我会破坏抽象。但是我有错误SyntaxError: missing ( before formal parameters
。有什么问题?
class Node{
constructor(value){
this.value = value;
this.next = null;
}
}
//IIFE function that return the class.
const SingleLinkedList = (() => { //Here use IIFE to create a new block scope for private variables
let _counts = new WeakMap();
let _head = new WeakMap();
let _tail = new WeakMap();
let _iterator = new WeakMap();
class SingleLinkedList {
constructor() {
_counts.set(this, 0);
_head.set(this, null);
_tail.set(this, null);
let instance = this;//closure
_iterator.set(this,(function* [Symbol.iterator](){
let n=_head.get(instance);
while(n){
yield n;
n = n.next;
}
}));
}
get counts() { //read only get accessor property
return _counts.get(this);
}
isEmpty() {
return !this.counts; //or return !_head.get(this)
}
//insert a new Node (in tail) with the desired value
push(value) {
let n = new Node(value);
if (this.isEmpty())
_head.set(this, n);
else {
let tail = _tail.get(this); //tail is a pointer to the node-tail
tail.next = n; //old tail-node will point to new node
}
//the following instructions are common to both the cases.
_tail.set(this, n); //set new tail
_counts.set(this, this.counts+1); //increment item counter
return this; //to allow multiple push call
}
//Generator to return the values
*[Symbol.iterator](){
let n = _head.get(this);
while(n){
yield n.value;
n=n.next;
}
}
//the the values of each node
print() {
let output = "";
for(let el of this)
output += `${el} `;
console.log(output);
}
}
return SingleLinkedList;
})();
let myLinkedList = new SingleLinkedList();
myLinkedList.push(3);
myLinkedList.push(5);
myLinkedList.print();
/*for(let x of myLinkedList)
console.log(x);*/
函数表达式不能有计算名称,该语法只允许在对象字面量成员或 class 成员上使用,因此请像这样定义 SingleLinkedList
的构造函数:
constructor() {
_counts.set(this, 0);
_head.set(this, null);
_tail.set(this, null);
let instance = this;//closure
_iterator.set(this, function* (){
// remove the computed name ---^
let n=_head.get(instance);
while(n){
yield n;
n = n.next;
}
});
}
也许更有用的是知道将来如何调试语法错误,这样您就不需要再问这样的问题了。如果按 F12 打开开发人员控制台,您会看到如下所示的日志:
单击 link 它将直接带您到错误位置:
只是为了好玩,这是我建议的重写,使用 ECMAScript 的一些更现代的功能,如 private fields:
class Node {
next = null;
constructor (value) {
this.value = value;
}
}
class SingleLinkedList {
#size = 0;
#head = null;
#tail = null;
// read only get accessor property
get size () {
return this.#size;
}
isEmpty () {
return this.#size === 0;
}
// insert a new Node (in tail) with the desired value
push (value) {
const node = new Node(value);
if (this.isEmpty()) {
this.#head = node;
} else {
this.#tail.next = node;
}
// the following instructions are common to both the cases.
this.#tail = node;
this.#size++;
// to allow multiple push call
return this;
}
// generator to return the values
*[Symbol.iterator] () {
for (let current = this.#head; current; current = current.next) {
yield current.value;
}
}
// the the values of each node
toString () {
return [...this].join(' ');
}
}
const myLinkedList = new SingleLinkedList();
myLinkedList.push(3).push(5);
console.log(myLinkedList.toString());
我正在自己构建一个链表。我试图在构造函数中将生成器分配为 key/value WeakMap 的值。 _iterator
是一个 WeakMap,因为它是我想用来对我的数据结构进行简单迭代的私有成员,但我不想让生成器在外部可见,否则我会破坏抽象。但是我有错误SyntaxError: missing ( before formal parameters
。有什么问题?
class Node{
constructor(value){
this.value = value;
this.next = null;
}
}
//IIFE function that return the class.
const SingleLinkedList = (() => { //Here use IIFE to create a new block scope for private variables
let _counts = new WeakMap();
let _head = new WeakMap();
let _tail = new WeakMap();
let _iterator = new WeakMap();
class SingleLinkedList {
constructor() {
_counts.set(this, 0);
_head.set(this, null);
_tail.set(this, null);
let instance = this;//closure
_iterator.set(this,(function* [Symbol.iterator](){
let n=_head.get(instance);
while(n){
yield n;
n = n.next;
}
}));
}
get counts() { //read only get accessor property
return _counts.get(this);
}
isEmpty() {
return !this.counts; //or return !_head.get(this)
}
//insert a new Node (in tail) with the desired value
push(value) {
let n = new Node(value);
if (this.isEmpty())
_head.set(this, n);
else {
let tail = _tail.get(this); //tail is a pointer to the node-tail
tail.next = n; //old tail-node will point to new node
}
//the following instructions are common to both the cases.
_tail.set(this, n); //set new tail
_counts.set(this, this.counts+1); //increment item counter
return this; //to allow multiple push call
}
//Generator to return the values
*[Symbol.iterator](){
let n = _head.get(this);
while(n){
yield n.value;
n=n.next;
}
}
//the the values of each node
print() {
let output = "";
for(let el of this)
output += `${el} `;
console.log(output);
}
}
return SingleLinkedList;
})();
let myLinkedList = new SingleLinkedList();
myLinkedList.push(3);
myLinkedList.push(5);
myLinkedList.print();
/*for(let x of myLinkedList)
console.log(x);*/
函数表达式不能有计算名称,该语法只允许在对象字面量成员或 class 成员上使用,因此请像这样定义 SingleLinkedList
的构造函数:
constructor() {
_counts.set(this, 0);
_head.set(this, null);
_tail.set(this, null);
let instance = this;//closure
_iterator.set(this, function* (){
// remove the computed name ---^
let n=_head.get(instance);
while(n){
yield n;
n = n.next;
}
});
}
也许更有用的是知道将来如何调试语法错误,这样您就不需要再问这样的问题了。如果按 F12 打开开发人员控制台,您会看到如下所示的日志:
单击 link 它将直接带您到错误位置:
只是为了好玩,这是我建议的重写,使用 ECMAScript 的一些更现代的功能,如 private fields:
class Node {
next = null;
constructor (value) {
this.value = value;
}
}
class SingleLinkedList {
#size = 0;
#head = null;
#tail = null;
// read only get accessor property
get size () {
return this.#size;
}
isEmpty () {
return this.#size === 0;
}
// insert a new Node (in tail) with the desired value
push (value) {
const node = new Node(value);
if (this.isEmpty()) {
this.#head = node;
} else {
this.#tail.next = node;
}
// the following instructions are common to both the cases.
this.#tail = node;
this.#size++;
// to allow multiple push call
return this;
}
// generator to return the values
*[Symbol.iterator] () {
for (let current = this.#head; current; current = current.next) {
yield current.value;
}
}
// the the values of each node
toString () {
return [...this].join(' ');
}
}
const myLinkedList = new SingleLinkedList();
myLinkedList.push(3).push(5);
console.log(myLinkedList.toString());