Last active
April 23, 2025 12:00
-
-
Save Kcko/3f1a530c7c71dd0f9344e9da270a5d2c to your computer and use it in GitHub Desktop.
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
// https://medium.com/@asierr/javascripts-function-prototype-tostring-just-got-useful-here-s-how-bc617fd7222c | |
function greet(name) { | |
return `Hello, ${name}`; | |
} | |
console.log(greet.toString()); | |
// result | |
"function greet(name) {\n return `Hello, ${name}`;\n}" | |
// any usage | |
const fn = x => x * 2; | |
const body = fn.toString(); | |
if (body.includes('x * 2')) { | |
console.log('Safe to optimize!'); | |
} | |
/* | |
Limitations | |
Doesn’t work on native functions (e.g., Math.max.toString() returns "function max() { [native code] }") | |
Some minifiers may still mangle formatting | |
Not recommended for secure comparisons (function source can be identical but semantically different) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment