使用现代ES6 javascript类的方法的本地临时变量

使用现代ES6 javascript类的方法的本地临时变量,javascript,es6-class,iife,Javascript,Es6 Class,Iife,在旧式javascript类中,您可以使用它为方法定义一些临时变量,这些变量是预先分配的,因此不必每次调用方法都调用new // constructor var MyClass = function(x,y,z){ this.pos=new vec3(x,y,z); } // method MyClass.prototype.vec_combine = ( function( a,b,c, up,fw) { var side = new vec3(); // allocated at

在旧式javascript类中,您可以使用它为方法定义一些临时变量,这些变量是预先分配的,因此不必每次调用方法都调用
new

// constructor
var MyClass = function(x,y,z){ this.pos=new vec3(x,y,z); } 

// method
MyClass.prototype.vec_combine = ( function( a,b,c, up,fw) {
    var side = new vec3(); // allocated at construction, not at invocation
    return function vec_combine( a,b,c, up,fw) { 
       cross_product( up,fw, side ); // side is perpendicular to up,fw
       side.normalize();
       this.pos.x = fw.x*a + up.x*b + side.x*c;
       this.pos.y = fw.y*a + up.y*b + side.y*c;
       this.pos.z = fw.z*a + up.z*b + side.z*c; 
    }
}() );
如何使用类似ES6的现代课程

class MyClass{
  constructor(x,y,z){ this.pos=new vec3(x,y,z); } 

  // method
  vec_combine( a,b,c, up,fw) {
    var side = new vec3(); // I don't want do "new" each invocation
    cross_product( up,fw, side ); // side is perpendicular to up,fw
    side.normalize();
       this.pos.x = fw.x*a + up.x*b + side.x*c;
       this.pos.y = fw.y*a + up.y*b + side.y*c;
       this.pos.z = fw.z*a + up.z*b + side.z*c; 
  }
}

你真的不能。您必须回过头来设置原型:

class Test { }

Test.prototype.test  = (s => () => console.log(s))("hi");

new Test().test()
为了稍微美化一下,您可以使用一个小助手重写这些方法:

const init = (cl, name, ...args) => cl.prototype[name] = cl.prototype[name](...args);
可用作:

class Test {
 test(s) {
   return function() {
     console.log(s);
   }
 }
}

init(Test, "test", "hi");
new Test().test();

但是,在大多数情况下,将变量的作用域局部扩展到整个类就足够了:

const Test = (s => {
   return class {
     test(){ console.log(s); }
   };
})("hi");
或:

。。。或者你只是把它放在上面,希望每个人都遵守“不要使用下划线变量”规则:


你不能按你的要求去做

但是,如果您已经在使用ES6,那么您可能正在使用模块。您也可以在模块中有一个未导出的变量

// Not exported
var side = new vec3(); // I don't want do "new" each invocation

export class MyClass{
  constructor(x,y,z){ this.pos=new vec3(x,y,z); } 

  // method
  vec_combine( a,b,c, up,fw) {
    cross_product( up,fw, side ); // side is perpendicular to up,fw
    side.normalize();
    this.pos.x = fw.x*a + up.x*b + side.x*c;
    this.pos.y = fw.y*a + up.y*b + side.y*c;
    this.pos.z = fw.z*a + up.z*b + side.z*c; 
  }
}
如果不使用模块,可以将整个类放入IIFE中

const MyClass =  (() => {
  var side = new vec3(); // I don't want do "new" each invocation

  return class {
    constructor(x,y,z){ this.pos=new vec3(x,y,z); } 

    vec_combine( a,b,c, up,fw) {
      cross_product( up,fw, side ); // side is perpendicular to up,fw
      side.normalize();
      this.pos.x = fw.x*a + up.x*b + side.x*c;
      this.pos.y = fw.y*a + up.y*b + side.y*c;
      this.pos.z = fw.z*a + up.z*b + side.z*c; 
    }
  }

}))

为什么不将
side
作为构造类变量
this.side=new vec3()
首先,没有什么可以阻止您在ES6中使用上层方法。我个人要做的是使用typescript并将其设置为
私有静态只读
。如果你不使用typscript,你就不会有
private
,如果你还没有使用ESNext,也不会有
readonly
(虽然可以通过适当的描述符更改来完成)。只是一个小的更新:显然,所有的更新在提案/草案/候选阶段都是最好的,所以除非你使用typscript,否则我认为这是不正确的(与其他建议的解决方案相比,更易于阅读和编写)。我忘记了vanilla JS中的您。IIFE的好处是,temp变量的定义是局部的,这使代码更易于阅读和维护。
// Not exported
var side = new vec3(); // I don't want do "new" each invocation

export class MyClass{
  constructor(x,y,z){ this.pos=new vec3(x,y,z); } 

  // method
  vec_combine( a,b,c, up,fw) {
    cross_product( up,fw, side ); // side is perpendicular to up,fw
    side.normalize();
    this.pos.x = fw.x*a + up.x*b + side.x*c;
    this.pos.y = fw.y*a + up.y*b + side.y*c;
    this.pos.z = fw.z*a + up.z*b + side.z*c; 
  }
}
const MyClass =  (() => {
  var side = new vec3(); // I don't want do "new" each invocation

  return class {
    constructor(x,y,z){ this.pos=new vec3(x,y,z); } 

    vec_combine( a,b,c, up,fw) {
      cross_product( up,fw, side ); // side is perpendicular to up,fw
      side.normalize();
      this.pos.x = fw.x*a + up.x*b + side.x*c;
      this.pos.y = fw.y*a + up.y*b + side.y*c;
      this.pos.z = fw.z*a + up.z*b + side.z*c; 
    }
  }