Suppress `Expected an identifier and instead saw 'default' (a reserved word)` in JSLint with Mongoose

心不动则不痛 提交于 2019-12-21 12:44:37

问题


I'm using jshint to validate my JavaScript files.

On the server-side I'm using node.js with Mongoose. In Mongoose I'm encouraged to write schemata in a fashion like:

var UserSchema = new mongoose.Schema({
    firstname : { type: String, default: '' }
});

When running linting, I get error:

Expected an identifier and instead saw 'default' (a reserved word).

Is there a way to suppress this error? I really would prefer that behaviour instead of writing:

var UserSchema = new mongoose.Schema({
    firstname : { type: String, "default": '' }
});

回答1:


You can also use the "es5" option to disable this from occurring.

See: http://jslinterrors.com/expected-an-identifier-and-instead-saw-a-a-reserved-word/




回答2:


default is indeed a reserved word in JavaScript (https://developer.mozilla.org/en/JavaScript/Reference/Reserved_Words). While technically you can use default in an object property name without any problems, you could end up having problems with that notation if your interpreter is strict (like lint is).

Simplest way to go forward: fix the problem by adding quotes. Lint won't whine you any longer. The code is two characters longer, but so what - linting passes and you are guaranteed to not have problems due to use of a reserved keyword.



来源:https://stackoverflow.com/questions/10936180/suppress-expected-an-identifier-and-instead-saw-default-a-reserved-word-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!