gpt4 book ai didi

javascript - point.dist = function () { ^ ReferenceError : points is not defined

转载 作者:行者123 更新时间:2023-12-02 22:17:45 26 4
gpt4 key购买 nike

此函数是从“JavaScript:权威指南”复制的,但由于某种原因它不起作用... **points.dist = function () { ^ ReferenceError: 点未定义**我对此很陌生,我不确定这样做是否正确

points.dist = function() {
var p1 = this[0];
var p2 = this[1];
var a = p2.x-p1.x;
var b = p2.y-p1.y;
return Math.sqrt(a*a + b*b);
};

console.log(points.dist())

最佳答案

您至少应该使用默认值定义点对象

// Arrays and objects can hold other arrays and objects:
var points = [ // An array with 2 elements.
{x:0, y:0}, // Each element is an object.
{x:1, y:1}
];

关于javascript - point.dist = function () { ^ ReferenceError : points is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59332470/

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