💾str.isKeyword
JS ⟩ primitives ⟩ String ⟩ custom members ⟩ .isKeyword
// import String extensions
const _StringExt = require('./String_ext.js');
'continue'.isKeyword, // true
'as'.isReservedWord, // false
replit:reserved words
// keywords
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