I am experimenting with 'hiding' my JavaScript code in such a way that if I were to make a library and share it, users could not see the implementation of functions. Out of curiosity, I tried doing the following:
console.log(Math.abs.toString());
This is what's logged to console:
function abs() { [native code] }
I want to know if the Math.abs source is accessible. My reason for wanting to know this directly ties to my second part of the question: how did the developers implement the behavior shown by this code, and if there are other ways to see implementations that are hindered, how were those hinderances implemented? Perhaps I wrote this in a confusing way, but basically I want to hide implementation of my functions, and it seems like the native Math library may be source for inspiration.
So, if I write the following code:
const behindTheScenes = a => {
a *= 2;
return a;
};
console.log(behindTheScenes.toString());
The following is logged to the console:
a => {
a *= 2;
return a;
}
This is what I expected, but I desire the log statement to not show this code. Let's imagine a *= 2
is a super secret thing I came up with myself.
Now, I've tried the following:
const behindTheScenes = a => {
a *= 2;
return a;
};
behindTheScenes.constructor.prototype.toString = () => {};
console.log(behindTheScenes.toString());
The following is logged to the console:
undefined
This is about what I expected. However, I do not know if this is the best solution. By that I mean, perhaps
- this solution is not foolproof
- it is not how the Math.abs behavior is implemented.
Also, I do not know exactly how to implement this in the file structure and such. You see, I am a student who is relatively new to JavaScript. All my coding using the language has been on the Codecademy website. That being said, when I'm using an IDE, would I put the behindTheScenes.constructor.prototype.toString = () => {}
line in the file in which behindTheScenes is defined?
I recognize that this use of 'file' and 'IDE' may not even make sense in the context of making a library, but again I simply am unexperienced, so please excuse this and answer my questions with the vocabulary which makes sense in the context.
Anyway, it may be that functions like Math.abs can be found on chrome's engine code Github or Firefox's engine code. But I do not know how to navigate these pages.