gpt4 book ai didi

javascript - 什么会导致 JavaScript "new"关键字失败?

转载 作者:行者123 更新时间:2023-11-29 23:57:06 25 4
gpt4 key购买 nike

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

所以我目前正在开发 WinJS Universal (UWP) 应用程序。我发现大多数时候我的电话(对特定类(class))都没有任何问题。但是,当我快速调用它时,返回值是类本身。我知道引用没有被更改,因为我可以在它下面放置一个 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;
}
};

如有任何帮助,我们将不胜感激。如果可以避免,我宁愿不必做一些 hack workarounds 来解决这个问题。

编辑:我刚刚在我的 Surface Pro 3 上运行了完全相同的代码,它运行没有任何问题。这似乎是我的桌面(Intel Core i7-6700K 处理器)的问题。这使我认为它是一个速度相关问题的假设永久存在,将查看 MS 是否有与此相关的 VS 的更新/补丁。

最佳答案

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

关于javascript - 什么会导致 JavaScript "new"关键字失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41338055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com