Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 根据一个实例更改所有实例的函数_Javascript_Node.js - Fatal编程技术网

Javascript 根据一个实例更改所有实例的函数

Javascript 根据一个实例更改所有实例的函数,javascript,node.js,Javascript,Node.js,我有一个叫做“Session”的类,其中有一个值,叫做“time”。 我想创建一个函数来改变一个实例的“时间”,而不管我创建了多少实例,增量都会在其他实例之间分配 示例:sess1.time=45,sess2.time=60,sess3.time=50 如果我给sess1.time加上5,其他两个都会减少2 以下是我目前的代码: 课堂研讨会{ 构造函数(名称、描述){ this.name=名称; this.desc=desc; } getWork(){ console.log(this.nam

我有一个叫做“Session”的类,其中有一个值,叫做“time”。 我想创建一个函数来改变一个实例的“时间”,而不管我创建了多少实例,增量都会在其他实例之间分配

示例:sess1.time=45,sess2.time=60,sess3.time=50 如果我给sess1.time加上5,其他两个都会减少2

以下是我目前的代码:

课堂研讨会{
构造函数(名称、描述){
this.name=名称;
this.desc=desc;
}
getWork(){
console.log(this.name+“”+this.desc);
}
}
课程扩展工作坊{
构造函数(名称、描述、时间){
超级(名称,描述);
这个时间=时间;
}
getSession(){
console.log(this.name+“”+this.desc+“”+this.time);
}
更改时间(新时间){
设delta=this.time-newTime;
this.time=newTime;
}
}
让sessionList=[
sess1=新会话(“鹰嘴豆泥”、“山药”、45),
sess2=新会话('Kabab','mmmmmm',70)
]
sessionList.forEach((会话)=>{
console.log(session.time);

});您可以保留会话实例数组:

 const sessions = []; // this is leaking though

 class Session extends Workshop {
   constructor(name, desc, time){
      super(name, desc);
      this.time = time;
      sessions.push(this); // collect all created sessions
   }

   getSession() {
      console.log(this.name + " " + this.desc + " " + this.time);
   }

   changeTime(newTime) {
      let delta = this.time - newTime;
      // apply the delta to all other sessions
      for(const other of sessions) {
       if(other === this) continue;
       other.time -= delta / (sessions.length - 1);
      }
      this.time = newTime;
   }
}

明确地管理一组
会话,而不是使用泄漏的全局会话,这可能是有益的。

尝试创建会话计数器,并将其传递给函数和有条件的if station,以便根据计数器值执行工作

 class Workshop {
    constructor(name, desc) {
      this.name = name;
      this.desc = desc;
    }
    getWork() {
      console.log(this.name + " " + this.desc);
    }
}
class Session extends Workshop {
    constructor(name, desc, time){
        super(name, desc);
        this.time = time;
    }
    getSession() {
        console.log(this.name + " " + this.desc + " " + this.time);
    }
    changeTime(newTime,i) {
        if(i==0){
            let delta = this.time + newTime;
            this.time = delta;
        }
        else {
            let delta = this.time -2;
            this.time = delta;
        }
        return this.time
    }
}
let sessionList = [
    sess1 = new Session('hummus', 'yamyam', 45),
    sess2 = new Session('Kabab', 'mmmmmm', 70)
 ]
var i=0;
sessionList.forEach((session) => {
    session.changeTime(5,i);
    i++;
    console.log(session.time);
});

值得注意的是,显式地管理它们比安静地、神秘地管理它们要好得多。如果这涉及到某种循环代码或web风格的请求/响应循环,那么像这样的全局代码可能会变得杂乱无章。