Last active
May 4, 2021 03:44
-
-
Save SheepTester/b6f31a7e6d3416e8c8f4078f75c6b2a2 to your computer and use it in GitHub Desktop.
JavaScript cheat sheet/reference guide generator for web development (for beginners)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Paste this into the console, and it'll console.log a quick overview of all the methods | |
| // that any beginning JavaScript programmer should know! | |
| // Tip: Run this somewhere like https://www.example.com/ so that the website's global | |
| // variables don't show up here. | |
| { | |
| const test = function () { | |
| const analysed = new Map() | |
| const entries = function * (obj) { | |
| for (const key of Object.getOwnPropertyNames(obj)) { | |
| yield [key, obj[key]] | |
| } | |
| for (const key of Object.getOwnPropertySymbols(obj)) { | |
| yield [`[${key.description}]`, obj[key]] | |
| } | |
| } | |
| const alphabet = 'abcdefghijklmnopqrstuvwxyz' | |
| function analyse (name, obj) { | |
| if (obj === null) { | |
| return [name + ' : null'] | |
| } else if (typeof obj === 'function' || typeof obj === 'object') { | |
| const arr = [] | |
| if (analysed.get(obj)) { | |
| return [`${name} === ${analysed.get(obj)}`] | |
| } | |
| analysed.set(obj, name || 'globalThis') | |
| if (typeof obj === 'function') { | |
| if (obj.prototype) { | |
| const base = name ? name + '#' : '' | |
| for (const [k, v] of entries(Object.getOwnPropertyDescriptors(obj.prototype))) { | |
| if (v.value) { | |
| arr.push(...analyse(base + k, v.value)) | |
| } else { | |
| arr.push(base + k + ' : getter') | |
| } | |
| } | |
| } else { | |
| return [`${name} : function(${[...alphabet.slice(0, obj.length)].join(', ')})`] | |
| } | |
| } else { | |
| for (const [k, v] of entries(obj)) { | |
| const key = k[0] === '[' | |
| ? name + k | |
| : name | |
| ? name + '.' + k | |
| : k | |
| arr.push(...analyse(key, v)) | |
| } | |
| if (arr.length === 0) { | |
| arr.push(`${name} : ${obj[Symbol.toStringTag] || obj.constructor.name}`) | |
| } | |
| } | |
| return arr | |
| } else { | |
| return [`${name} : ${typeof obj}`] | |
| } | |
| } | |
| console.log(analyse('', globalThis).sort().join('\n')) | |
| } | |
| // setTimeout used to avoid showing console API functions | |
| setTimeout(eval(test)) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Revision 1
Initial revision.
Revision 2
Avoid duplicating
globalThis(globalThiswas incorrectly cached)Revision 3
Do not show globals from the command line API; avoid polluting the global namespace
Revision 4
Show argument count for functions
Revision 5
Alphabetise everything
Revision 6
Empty objects will now show their prototype
Revision 7
Show prototype name of objects with no owned properties; better symbol property key name display