这个 Javascript 对象键的用途是什么?

What is the purpose of this Javascript object key?

考虑 matter.js 中的这个函数:

/**
 * Creates a new set of axes from the given vertices.
 * @method fromVertices
 * @param {vertices} vertices
 * @return {axes} A new axes from the given vertices
 */
Axes.fromVertices = function(vertices) {
    var axes = {};

    // find the unique axes, using edge normal gradients
    for (var i = 0; i < vertices.length; i++) {
        var j = (i + 1) % vertices.length,
            normal = Vector.normalise({
                x: vertices[j].y - vertices[i].y,
                y: vertices[i].x - vertices[j].x
            }),
            gradient = (normal.y === 0) ? Infinity : (normal.x / normal.y);

        // limit precision
        gradient = gradient.toFixed(3).toString();
        axes[gradient] = normal;
    }

    return Common.values(axes);
};

为了完成,这里是 Common.values() 函数:

/**
 * Returns the list of values for the given object.
 * @method values
 * @param {} obj
 * @return {array} Array of the objects property values
 */
Common.values = function(obj) {
    var values = [];

    if (Object.keys) {
        var keys = Object.keys(obj);
        for (var i = 0; i < keys.length; i++) {
            values.push(obj[keys[i]]);
        }
        return values;
    }

    // avoid hasOwnProperty for performance
    for (var key in obj)
        values.push(obj[key]);
    return values;
};

我不太明白坐标区对象的结构。我没有看到 axes[gradient] = normal 代码的意义,因为 Common.values() function 只有 returns 值,因此永远不会返回梯度?

是的,永远不会返回 gradient,只有 normal 值。将它们塞进那个对象的整个过程是为了避免重复,正如评论所解释的:

// find the unique axes, using edge normal gradients

如果您有多个具有相似(最多三位数)梯度的法线,则只会返回最后一个。