Js在swift中实现了类似协议委托的东西

时间:2017-06-05 22:16:36

标签: javascript delegates

在js中是否存在类似协议委托的内容?类似的东西:

class a{
    somefunction();//<<== Is called from here
}

class b{
    class_A = new a();

    somefunction(){//Code is implemented here
        //Do something
    }
}

1 个答案:

答案 0 :(得分:0)

你的意思是这样的

class a {

  constructor() {
    this.b = 100000;

    this.a = {
      *[Symbol.iterator]() {
        let pre = 0,
          cur = 1
        for (;;) {
          [pre, cur] = [cur, pre + cur]
          yield cur
        }
      }
    }

  }

  get more() {
    var items = new b(this.a, this.b);
    return items.some;
  }

};

class b {

  constructor(a, b) {
    this.a = a;
    this.b = b;
    let k = 1;
  }


  get some() {
    for (let n of this.a) {
      if (n > this.b)
        break
      console.log(n)

    }
  }
}


var other = new a();
console.log(other.more);