2016-12-27 10 views
2

:私は、だから私は現在、WinJSユニバーサル(UWP)アプリケーションに取り組んでいますES6(クラス)JavaScriptの「新しい」キーワードが失敗する原因は何ですか?

を使用しています。私は、(特定のクラスへの)私の呼び出しが問題なく動作することがほとんどの場合わかっています。しかし、私がそれを迅速に呼び出すときには、戻り値はクラスそのものです。私は参照が変更されていないことを知っています。なぜなら、そのすぐ下にwhileループを置くことができるからです。それはある種のタイミング問題のようです。

UWP WinJS application

私は、次のコードに解除されます:

var crop = new $js.Vector2(xCrop, yCrop); 

while (!(crop instanceof $js.Vector2)) { 
    crop = new $js.Vector2(xCrop, yCrop); 
} 

if (!(crop instanceof $js.Vector2)) { 
    debugger; // This is never hit and the program continues as normal 
} 

プログラムが通常の実行だまいります。私は、次の手順を実行している場合しかし

var crop = new $js.Vector2(xCrop, yCrop); 

if (!(crop instanceof $js.Vector2)) { 
    debugger; // This is being hit (not 100% chance, only in rapid use) 
} 

Crop is being set to the class rather than an instance

。また、コードのその部分を回避するためにブレークポイントを使用すると、完全に正常に動作します。これは私にタイミングの問題であると信じさせるものです。私はこれがマイクロソフト側のバグか私の側のバグかを判断しようとしているだけです。任意の助けをいただければ幸いです

/** 
* A basic 2 dimensional vector 
* @class 
*/ 
$js.Vector2 = class { 

    /** 
    * @constructor 
    * @param {number} [x=0] The x dimension of the vector 
    * @param {number} [y=0] The y dimension of the vector 
    */ 
    constructor(x, y) { 
     /** 
     * The x dimension of this vector 
     * @type {number} 
     */ 
     this.x = x || 0; 

     /** 
     * The y dimension of this vector 
     * @type {number} 
     */ 
     this.y = y || 0; 
    } 

    /** 
    * Copys the x and y dimension of a $js.Vector2 to this one 
    * @param {number} x 
    * @param {number} y 
    */ 
    set(x, y) { 
     if (x != null) { 
      this.x = x; 
     } 

     if (y != null) { 
      this.y = y; 
     } 
    } 

    /** 
    * Transposes this vector by another vector by shifting (adding) 
    * @param {$js.Vector2} vector The vector to be added to this vector 
    */ 
    move(vector) { 
     this.x += vector.x; 
     this.y += vector.y; 
    } 

    /** 
    * Get's the magnitude (pythagorean theorem) of this vector (the length of the hypotenuse of the right triangle produced by this vector) 
    * @return {number} The length of the hypotenuse 
    */ 
    get magnitude() { 
     return Math.sqrt((this.x * this.x) + (this.y * this.y)) 
    } 

    /** 
    * Get's the dot product of this vector and another 
    * @param {$js.Vector2} vector The vector to be multiplied with this vector 
    * @return {number} The result of dot product (vector multiplication) 
    */ 
    dot(vector) { 
     return (this.x * vector.x) + (this.y * vector.y); 
    } 

    /** 
    * This will return a new normalized $js.Vector2 of this vector 
    * @return {$js.Vector2} The normalized $js.Vector2 
    */ 
    get normalized() { 
     var tmp = new $js.Vector2(this.x, this.y); 

     var mag = this.magnitude; 
     tmp.x = tmp.x/mag; 
     tmp.y = tmp.y/mag; 

     return tmp; 
    } 

    /** 
    * Will get the distance between this vector and another supplied vector 
    * @param {$js.Vector2} vector 
    * @return {number} The distance between this $js.Vector2 and the supplied $js.Vector2 
    */ 
    distance(vector) { 
     return Math.sqrt(((vector.x - this.x) * (vector.x - this.x)) + ((this.y - vector.y) * (this.y - vector.y))); 
    } 

    /** 
    * Will subtract this vector from another vector 
    * @param {$js.Vector2} vector 
    * @return {$js.Vector2} The result of this vector subtracted by a supplied vector (in that order) 
    */ 
    difference(vector) { 
     return new $js.Vector2((this.x - vector.x), (this.y - vector.y)); 
    } 

    /** 
    * Will add this vector from another vector 
    * @param {$js.Vector2} vector 
    * @return {$js.Vector2} The result of this vector added by a supplied vector 
    */ 
    sum(vector) { 
     return new $js.Vector2((this.x + vector.x), (this.y + vector.y)); 
    } 

    /** 
    * Will check if this vector's components are equal to the supplied vectors 
    * @param {$js.Vector2} vector The vector to compare against 
    * @return {boolean} <c>true</c> if the x, y, and z of both vectors are the same value otherwise <c>false</c> 
    */ 
    equals(vector) { 
     if (!(vector instanceof $js.Vector2)) { 
      return false; 
     } 

     return this.x === vector.x && this.y === vector.y; 
    } 
}; 

は、ここに私の$ js.Vector2コードです。回避できれば、この問題を解決するためにいくつかのハックを回避する必要はありません。

編集:私はSurface Pro 3でこれと同じ正確なコードを実行しましたが、問題なく動作しました。私のデスクトップ(インテルCore i7-6700Kプロセッサ)に問題があるようです。これはスピードに関連する問題であるという私の前提を永続させ、VSに関連するMSのアップデート/パッチがあればそれを見るでしょう。

+0

'while'ループの予想される結果は何ですか? – guest271314

+0

@ guest271314 'while'ループの目的は、' new $ js.Vector2'の呼び出しに何も問題がないという私の理論を確認することです。私がその呼び出しを持っていなければ、プログラムは 'debugger'で(イメージに見られるように)壊れます。しかし、私がその呼び出しを追加すると、動作を開始し、決して 'debugger'行にヒットしません。したがって、単にコンストラクタを繰り返し呼び出すだけで、クラス/関数への参照ではなく、オブジェクトに評価されます。 – baflink

+0

'difference'と' sum'の中で 'new $ js.Vector2'を呼び出す目的は何ですか? – guest271314

答えて

関連する問題