Stack 和 Sets 实现(this 与 var)

Stack and Sets implementation (this vs var)

提问人:Trush P 提问时间:7/3/2021 更新时间:7/3/2021 访问量:32

问:

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

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

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

My Set 实现:

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))
javascript oop 数据结构 这个 var

评论

1赞 VLAZ 7/3/2021
this.collection.values(); -> this.values();或。否则,您将调用 Array#valuesthis.collection

答:

1赞 CertainPerformance 7/3/2021 #1

this.collection包含数组。调用返回数组迭代器。数组迭代器没有方法。Array.prototype.valuesforEach

只需使用 而不是 ,您就可以在结果上使用 (referencing )。this.collectionthis.collection.valuesforEachArray.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) {

评论

1赞 Sebastian Simon 7/3/2021
“数组迭代器还没有 forEach 方法”——还没有