【问题标题】:What could cause JavaScript "new" keyword to fail?什么可能导致 JavaScript “new” 关键字失败?
【发布时间】:2017-05-11 07:41:28
【问题描述】:

注意:我使用的是 ES6(类)

所以我目前正在开发一个 WinJS Universal (UWP) 应用程序。我发现大多数时候我的电话(对特定班级)都没有任何问题。但是,当我快速调用它时,返回值就是类本身。我知道引用没有被更改,因为我可以在它下面放一个 while 循环,它会再次开始工作。这似乎是某种时间问题。

我将打破以下代码:

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

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

但是,如果我执行以下操作:

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 更新/补丁。

【问题讨论】:

  • while 循环的预期结果是什么?
  • @guest271314 while 循环的目的是为了证实我的理论,即调用new $js.Vector2 没有任何问题。如果我没有那个电话,程序将在debugger 上中断(如图所示)。但是,当我添加该调用时,它开始工作,我从未打过debugger 行。因此,通过简单地一遍又一遍地调用构造函数,它最终将评估为一个对象,而不是对类/函数的引用。
  • differencesum内调用new $js.Vector2的目的是什么?
  • @guest271314 只是一个错字,应该在新关键字之前有返回(如注释文档所示)。我已经更新了代码/帖子并进行了测试。
  • 没有什么会导致这种情况,除非您在某个时候使用crop = $js.Vector2。尚不清楚为什么它毕竟不是const。您确定这是一个真正的问题,而不是调试器中的故障吗?

标签: javascript visual-studio ecmascript-6 uwp instantiation


【解决方案1】:

这可能是一个已知问题: https://github.com/Microsoft/ChakraCore/issues/1496 https://github.com/Microsoft/ChakraCore/pull/1577 看看它是否在 Windows Insider Preview 上重现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    相关资源
    最近更新 更多