2016-12-27 50 views
2

注意:我使用ES6(類)什麼可能導致JavaScript「新」關鍵字失敗?

所以我目前工作的一個WinJS通用(UWP)應用程序。我發現大部分時間我的電話(對特定班級)沒有任何問題。但是,當我快速調用它時,返回值就是類本身。我知道引用沒有被改變,因爲我可以在它下面放一個while循環,它會再次開始工作。這似乎是某種時機問題。

UWP WinJS application

我將在下面的代碼破解:

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

但是,如果我做了以下內容:

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 
} 

該計劃將繼續它的正常執行。另外,當我在代碼的這一部分中使用斷點來逐步完成它時,它工作得很好;這導致我相信這是一些時間問題。我只是想弄清楚這是微軟方面還是我方方面的錯誤。

這裏是我的$ js.Vector2代碼:

/** 
* 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; 
    } 
}; 

任何幫助,將不勝感激。如果可以避免的話,我寧願不需要採取一些黑客辦法來解決這個問題。

編輯:我只是在Surface Pro 3上運行同樣的代碼,它沒有任何問題。這似乎是我的臺式機(英特爾酷睿i7-6700K處理器)的問題。這延續了我作爲一個速度相關問題的假設,將會看到是否有與MS有關的VS的更新/補丁。

+0

'while'循環的預期結果是什麼? – guest271314

+0

@ guest271314「while」循環的目的是爲了證實我的理論,即對'new $ js.Vector2'的調用沒有任何問題。如果我沒有這個調用,程序將在「調試器」上中斷(如圖所示)。但是當我添加這個調用時,它開始工作,我從來沒有打過'debugger'這行。所以通過一次又一次地調用構造函數,它最終將評估一個對象而不是對類/函數的引用。 – baflink

+0

在'difference'和'sum'內調用'new $ js.Vector2'的目的是什麼? – guest271314

回答

相關問題