๐พstr.isReservedWord
JS โฉ primitives โฉ String โฉ custom members โฉ .isResevedWord
// import String extensions
const _StringExt = require('./String_ext.js');
'continue'.isKeyword, // true
'as'.isReservedWord, // false
replit๏ผreserved words
// ๐ String_ext.js
const keywords = [
'as', 'async', 'await', 'break',
'case', 'catch', 'class', 'const',
'continue', 'debugger', 'default', 'delete',
'do', 'else', 'enum', 'export',
'extends', 'false', 'finally', 'for',
'from', 'function', 'get', 'if',
'implements', 'import', 'in', 'instanceof',
'interface', 'let', 'meta', 'new',
'null', 'of', 'package', 'private',
'protected', 'public', 'return', 'set',
'static', 'super', 'switch', 'target',
'this', 'throw', 'true', 'try',
'typeof', 'var', 'void', 'while',
'with', 'yield'
];
// reserved words
const reservedWords = [
'await', 'break', 'case',
'catch', 'class', 'const',
'continue', 'debugger', 'default',
'delete', 'do', 'else',
'enum', 'export', 'extends',
'false', 'finally', 'for',
'function', 'if', 'import',
'in', 'instanceof', 'new',
'null', 'return', 'super',
'switch', 'this', 'throw',
'true', 'try', 'typeof',
'var', 'void', 'while',
'with', 'yield'
];
// getters for String
Object.defineProperties(String.prototype, {
// ๐ธ str.isKeyword
isKeyword: {
get() { return keywords.includes(this.valueOf()) }
// ^^^^^^^^^
// ๐งจ ้ทๅ
// -----------------------------------------------
// get() { return keywords.includes(this) }
// ^^^^
// โ `this` will be "wrapped" in a `String` object,
// hence `false` is "always" returned.
},
// ๐ธ str.isReservedWord
isReservedWord: {
get() { return reservedWords.includes(this.valueOf()) }
// ^^^^^^^^^
// ๐งจ ้ทๅ
// -----------------------------------------------
// get() { return keywords.includes(this) }
// ^^^^
// โ `this` will be "wrapped" in a `String` object,
// hence `false` is "always" returned.
},
});
// export
module.exports = {keywords, reservedWords};
Last updated