Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript points.dist=函数(){^ReferenceError:未定义点_Javascript_Function - Fatal编程技术网

Javascript points.dist=函数(){^ReferenceError:未定义点

Javascript points.dist=函数(){^ReferenceError:未定义点,javascript,function,Javascript,Function,此函数是从“JavaScript:The Definitive Guide”复制而来的,但由于某些原因,它不起作用…**points.dist=function(){^ReferenceError:未定义点** 我在这方面是新手,我不确定是否有人做得对 points.dist=函数(){ var p1=该[0]; var p2=此[1]; 变量a=p2.x-p1.x; var b=p2.y-p1.y; 返回Math.sqrt(a*a+b*b); }; console.log(points.dis

此函数是从“JavaScript:The Definitive Guide”复制而来的,但由于某些原因,它不起作用…
**points.dist=function(){^ReferenceError:未定义点**
我在这方面是新手,我不确定是否有人做得对

points.dist=函数(){
var p1=该[0];
var p2=此[1];
变量a=p2.x-p1.x;
var b=p2.y-p1.y;
返回Math.sqrt(a*a+b*b);
};
console.log(points.dist())
var点=[
{x:0,y:0},
{x:1,y:1}
];
points.dist=函数(){
var p1=该[0];
var p2=此[1];
变量a=p2.x-p1.x;
var b=p2.y-p1.y;
返回Math.sqrt(a*a+b*b);
};

console.log(points.dist());//=>1.414两点之间的距离
您至少应该使用默认值定义points对象

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

它告诉您,
没有定义。您在代码中没有显示定义它的任何内容,因此错误似乎是正确的。可能您缺少了前面定义
的书中的一些代码?