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

Safari浏览器使用JavaScript Class语法报错,求解决方案

Fixing "SyntaxError: Use of reserved word 'class'" in Safari

Hey there! The issue you're hitting is that older versions of Safari (specifically Safari 8 and below) don't support ES6's class syntax—class was a new reserved keyword introduced in ES6, and Safari didn't add full support for it until version 9. To make your code work across all Safari versions, you'll need to convert your ES6 class to an ES5 constructor function, which is compatible with older browsers.

Here's the updated code:

function FileSet(id, path, folder, children, all, isfolder) {
  // Initialize instance properties just like you would in the class constructor
  this.id = id;
  this.path = path;
  this.folder = folder;
  this.children = children;
  this.all = all;
  this.isfolder = isfolder;
}

// If your original class had prototype methods (e.g., a method shared across all instances),
// you'd add them to the constructor's prototype like this:
// FileSet.prototype.yourMethodName = function() {
//   // Your method logic here
// };

Key points about the conversion:

  • Replace the class declaration with a standard function constructor that uses the same parameters as your original constructor
  • Assign instance properties using this.propertyName = value inside the constructor (matching how you'd set them in the ES6 class constructor)
  • Any prototype methods from the original class get attached to FileSet.prototype as function expressions

This adjusted code will run smoothly in Safari (and all other browsers) without throwing that syntax error.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:07:59