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": '' }
});
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/
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