我是JavaScript新手。
我正在React应用程序中编写一个JavaScript文件,并以此方式在一个类中声明一个私有函数notifyObservers
:
class Subject {
constructor(idOfDevice) {
//...
}
var notifyObservers = function (data) {
this.observers.forEach(obs => obs.notify(data));
}
getFrequency(idOfDevice) {
//...
}
}
如我所见,建议这样做。我在var
表示法上遇到此错误:
Unexpected token. A constructor, method, accessor, or property was expected.ts(1068)
我在{
方法的getFrequency
上遇到错误:
';' expected.ts(1005)
我不明白为什么,因为看不到TypeScript。而且我敢肯定,这只是一个愚蠢的菜鸟错误,但我似乎无法弄清楚。
这是我在package.json
文件中所拥有的:
{
"name": "name",
"version": "0.1.0",
"private": true,
"homepage": "http://localhost:3000",
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.4.0",
"@testing-library/user-event": "^7.2.1",
"avsc": "^5.4.18",
"axios": "^0.19.2",
"chart.js": "^2.9.3",
"dotenv": "^8.2.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-particles-js": "^2.7.1",
"react-router-dom": "^5.1.2",
"react-scripts": "3.3.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@iconify/icons-ant-design": "^1.0.6",
"@iconify/icons-bx": "^1.0.0",
"@iconify/icons-fe": "^1.0.3",
"@iconify/icons-ic": "^1.0.8",
"@iconify/icons-jam": "^1.0.3",
"@iconify/react": "^1.1.3"
}
}
我的@typescript-eslint
中仍然有一个node_modules
文件夹,而我的package-lock.json
中有这个文件夹:
"@typescript-eslint/eslint-plugin": {
"version": "2.27.0",
"resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.27.0.tgz",
"integrity": "sha512-/my+vVHRN7zYgcp0n4z5A6HAK7bvKGBiswaM5zIlOQczsxj/aiD7RcgD+dvVFuwFaGh5+kM7XA6Q6PN0bvb1tw==",
"requires": {
"@typescript-eslint/experimental-utils": "2.27.0",
"functional-red-black-tree": "^1.0.1",
"regexpp": "^3.0.0",
"tsutils": "^3.17.1"
}
},
"@typescript-eslint/experimental-utils": {
"version": "2.27.0",
"resolved": "https://registry.npmjs.org/@typescript-eslint/experimental-utils/-/experimental-utils-2.27.0.tgz",
"integrity": "sha512-vOsYzjwJlY6E0NJRXPTeCGqjv5OHgRU1kzxHKWJVPjDYGbPgLudBXjIlc+OD1hDBZ4l1DLbOc5VjofKahsu9Jw==",
"requires": {
"@types/json-schema": "^7.0.3",
"@typescript-eslint/typescript-estree": "2.27.0",
"eslint-scope": "^5.0.0",
"eslint-utils": "^2.0.0"
},
"dependencies": {
"eslint-utils": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/eslint-utils/-/eslint-utils-2.0.0.tgz",
"integrity": "sha512-0HCPuJv+7Wv1bACm8y5/ECVfYdfsAm9xmVb7saeFlxjPYALefjhbYoCkBjPdPzGH8wWyTpAez82Fh3VKYEZ8OA==",
"requires": {
"eslint-visitor-keys": "^1.1.0"
}
}
}
},
"@typescript-eslint/parser": {
"version": "2.27.0",
"resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-2.27.0.tgz",
"integrity": "sha512-HFUXZY+EdwrJXZo31DW4IS1ujQW3krzlRjBrFRrJcMDh0zCu107/nRfhk/uBasO8m0NVDbBF5WZKcIUMRO7vPg==",
"requires": {
"@types/eslint-visitor-keys": "^1.0.0",
"@typescript-eslint/experimental-utils": "2.27.0",
"@typescript-eslint/typescript-estree": "2.27.0",
"eslint-visitor-keys": "^1.1.0"
}
},
"@typescript-eslint/typescript-estree": {
"version": "2.27.0",
"resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-2.27.0.tgz",
"integrity": "sha512-t2miCCJIb/FU8yArjAvxllxbTiyNqaXJag7UOpB5DVoM3+xnjeOngtqlJkLRnMtzaRcJhe3CIR9RmL40omubhg==",
"requires": {
"debug": "^4.1.1",
"eslint-visitor-keys": "^1.1.0",
"glob": "^7.1.6",
"is-glob": "^4.0.1",
"lodash": "^4.17.15",
"semver": "^6.3.0",
"tsutils": "^3.17.1"
}
}
即使我似乎无法删除这些内容:如果我这样做,它们也会在我npm start
启动我的应用程序后立即重新出现。
我已通过卸载了打字稿
npm uninstall -g typescript
以及通过
npm uninstall --save typescript
此外,添加
"typescript.validate.enable": false,
对我的settings.json
文件没有帮助。
答案 0 :(得分:1)
VS Code使用TypeScript为其JavaScrip IntelliSense提供动力。这里的错误文本会告诉您所需的一切,ts
位无关紧要。
错误消息是:
A constructor, method, accessor, or property was expected
这告诉您类只能包含那些元素。但是,您的代码在类内声明了var
,而vars不在期望的元素列表中。这意味着您的代码存在语法错误,并且不是有效的JavaScript。
有关如何在JS类中声明私有属性的信息,请参见答案like this