You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JS坐标数据结构报错:Uncaught SyntaxError: Unexpected identifier 'Point'

问题分析与修正代码

核心错误原因

你代码里用了capability这个JavaScript不存在的关键字来定义构造函数和普通函数,这直接导致了语法错误Uncaught SyntaxError: Unexpected identifier 'Point'。JavaScript里定义构造函数用function或ES6的class,普通函数直接用function声明即可。

其他问题点

  1. rotate函数参数错误:你定义的参数是(points, point),但函数内部使用了未声明的angle,且调用时传的是角度值5,应该把参数改为(points, angle)。
  2. 角度单位不匹配:Math.cos()和Math.sin()接收的是弧度值,你传入的是角度,需要转换为弧度(角度 × π/180)。
  3. 变量名不一致:调用translate和rotate时用了未定义的points变量,应该用你声明的focuses数组。

修正后的完整代码

// 用function定义Point构造函数(ES5写法)
function Point(x, y) {
  this.x = x;
  this.y = y;
}

// 初始化点数组
var focuses = [new Point(1, 1), new Point(2, 2), new Point(3, 3), new Point(4, 4), new Point(5, 5)];

// 平移函数:修正capability为function
function translate(points, dx, dy) {
  return points.map(function(point) {
    return new Point(point.x + dx, point.y + dy);
  });
}

// 旋转函数:修正参数、角度转换、capability关键字
function rotate(points, angle) {
  // 将角度转为弧度
  var radian = angle * Math.PI / 180;
  return points.map(function(point) {
    return new Point(
      point.x * Math.cos(radian) - point.y * Math.sin(radian),
      point.x * Math.sin(radian) + point.y * Math.cos(radian)
    );
  });
}

// 调用函数:修正变量名为focuses
focuses = translate(focuses, 3, 0);
focuses = rotate(focuses, 5);

// 打印结果验证
console.log(focuses);

如果你习惯ES6语法,也可以用class写法:

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
}

const focuses = [new Point(1,1), new Point(2,2), new Point(3,3), new Point(4,4), new Point(5,5)];

const translate = (points, dx, dy) => points.map(point => new Point(point.x + dx, point.y + dy));

const rotate = (points, angle) => {
  const radian = angle * Math.PI / 180;
  return points.map(point => new Point(
    point.x * Math.cos(radian) - point.y * Math.sin(radian),
    point.x * Math.sin(radian) + point.y * Math.cos(radian)
  ));
}

focuses = translate(focuses, 3, 0);
focuses = rotate(focuses, 5);
console.log(focuses);

内容的提问来源于stack exchange,提问作者Asim Malik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 23:55:19