Stack 和 Sets 实现(this vs var)

Stack and Sets implementation (this vs var)

我正在观看 freeCodeCamps 数据结构和算法 JavaScript 实现视频 (https://www.youtube.com/watch?v=t2CEgPsws3U),我注意到 Stacks 在其实现集合时使用了 this 关键字 (4:49)使用 var (10:06).

我尝试实现自己的 Sets 实现并收到错误 TypeError: firstSet.forEach is not a function

  1. 这是因为我使用了 this 而不是 var 还是我在实现上犯了错误?
  2. 为什么堆栈实现使用 this 而不是 var

我的设置实现:

let mySet = function(){
  this.collection = [];

  this.has = (elem) =>{
    return (this.collection.indexOf(elem)!==-1)
  }

  this.values = ()=>{
    return this.collection;
  }

  this.add = (elem)=>{
    if(!this.has(elem)){
      this.collection.push(elem)
      return true;
    }
    return false
  }

  this.remove = (elem)=>{
    if(this.has(elem)){
      let idx = this.collection.indexOf(elem);
      this.collection.splice(idx,1);
      return true;
    }
    return false;
  }

  this.size = ()=>{
    return this.collection.length;
  }

  this.union = (secondSet)=>{
    let unionSet = new mySet();
    let firstSet = this.collection.values();
    let second = secondSet.values();
    firstSet.forEach(function(e){
      unionSet.add(e);
    })
    second.forEach(function(e){
      unionSet.add(e)
    })
    return unionSet;
  }

  this.intersection = (secondSet)=>{
    let intersectionSet = new mySet();
    let firstSet = this.collection.values();
    let second = secondSet.values();
    firstSet.forEach(function(e){
      if(second.has(e)){
        intersectionSet.add(e);
      }
    })
    return intersectionSet;
  }

  this.difference = (secondSet)=>{
    let differenceSet = new mySet();
    let firstSet = this.collection.values();
    let second = secondSet.values();
    firstSet.forEach(function(e){
      if(!second.has(e)){
        differenceSet.add(e)
      }
    })
    return differenceSet;
  }

  this.subset = (secondSet)=>{
    let firstSet = this.collection.values();
    let second = secondSet.values();
    firstSet.every(function(e){
      if(second.has(e)){
        return true;
      }
    })
  }
}
let mySet1 = new mySet();
let mySet2 = new mySet();

mySet1.add(2)
mySet1.add(3)
mySet1.add(1)
mySet1.add(2)
mySet1.add(5)
mySet2.add(2)
mySet2.add(4);
mySet2.add(6);
console.log(mySet2.union(mySet1))

this.collection 包含数组。调用 Array.prototype.values returns array iterator。数组迭代器没有 forEach 方法。

只需使用 this.collection 而不是 this.collection.values,您就可以对结果使用 forEach(引用 Array.prototype.forEach)。

let mySet = function(){
  this.collection = [];

  this.has = (elem) =>{
    return (this.collection.indexOf(elem)!==-1)
  }

  this.values = ()=>{
    return this.collection;
  }

  this.add = (elem)=>{
    if(!this.has(elem)){
      this.collection.push(elem)
      return true;
    }
    return false
  }

  this.remove = (elem)=>{
    if(this.has(elem)){
      let idx = this.collection.indexOf(elem);
      this.collection.splice(idx,1);
      return true;
    }
    return false;
  }

  this.size = ()=>{
    return this.collection.length;
  }

  this.union = (secondSet)=>{
    let unionSet = new mySet();
    let firstSet = this.collection;
    let second = secondSet.values();
    firstSet.forEach(function(e){
      unionSet.add(e);
    })
    second.forEach(function(e){
      unionSet.add(e)
    })
    return unionSet;
  }

  this.intersection = (secondSet)=>{
    let intersectionSet = new mySet();
    let firstSet = this.collection.values();
    let second = secondSet.values();
    firstSet.forEach(function(e){
      if(second.has(e)){
        intersectionSet.add(e);
      }
    })
    return intersectionSet;
  }

  this.difference = (secondSet)=>{
    let differenceSet = new mySet();
    let firstSet = this.collection.values();
    let second = secondSet.values();
    firstSet.forEach(function(e){
      if(!second.has(e)){
        differenceSet.add(e)
      }
    })
    return differenceSet;
  }

  this.subset = (secondSet)=>{
    let firstSet = this.collection.values();
    let second = secondSet.values();
    firstSet.every(function(e){
      if(second.has(e)){
        return true;
      }
    })
  }
}
let mySet1 = new mySet();
let mySet2 = new mySet();

mySet1.add(2)
mySet1.add(3)
mySet1.add(1)
mySet1.add(2)
mySet1.add(5)
mySet2.add(2)
mySet2.add(4);
mySet2.add(6);
console.log(mySet2.union(mySet1).collection)

或者调用迭代器。

for (const e of firstSet) {