Are there legitimate uses for JavaScript's “with” statement?

后端 未结 30 2224
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 04:22

Alan Storm\'s comments in response to my answer regarding the with statement got me thinking. I\'ve seldom found a reason to use this particular language feature, and had ne

30条回答
  •  死守一世寂寞
    2020-11-22 05:19

    For some short code pieces, I would like to use the trigonometric functions like sin, cos etc. in degree mode instead of in radiant mode. For this purpose, I use an AngularDegreeobject:

    AngularDegree = new function() {
    this.CONV = Math.PI / 180;
    this.sin = function(x) { return Math.sin( x * this.CONV ) };
    this.cos = function(x) { return Math.cos( x * this.CONV ) };
    this.tan = function(x) { return Math.tan( x * this.CONV ) };
    this.asin = function(x) { return Math.asin( x ) / this.CONV };
    this.acos = function(x) { return Math.acos( x ) / this.CONV };
    this.atan = function(x) { return Math.atan( x ) / this.CONV };
    this.atan2 = function(x,y) { return Math.atan2(x,y) / this.CONV };
    };
    

    Then I can use the trigonometric functions in degree mode without further language noise in a with block:

    function getAzimut(pol,pos) {
      ...
      var d = pos.lon - pol.lon;
      with(AngularDegree) {
        var z = atan2( sin(d), cos(pol.lat)*tan(pos.lat) - sin(pol.lat)*cos(d) );
        return z;
        }
      }
    

    This means: I use an object as a collection of functions, which I enable in a limited code region for direct access. I find this useful.

提交回复
热议问题