This question is in reference to this old question Where-can-i-find-javascript-native-functions-source-code
The answer on that page says, that the source code is in c
or c++
but I am curious as to why the source (definition) is in those languages? I mean they are JS functions definitions for e.g toString()
method. It's a JavaScript function so its definition must be written using Javascript syntax.
toString;
in chrome console outputs function toString() { [native code] }
.
If it's a user-defined function then you can see the definition but not toString()
or for that matter other in-built functions
after all they are just function/methods that must be defined in JavaScript syntax for the engine to interpret them correctly.
I hope you can understand what point I am trying to make.
As pointed out in the comments, you have a fundamental misunderstanding of how JavaScript works.
JavaScript is a scripting language, in the purest sense of that term, i.e. it is meant to script a host environment. It is meant to be embedded in a larger system (in this case, a web browser written in C/C++) to manipulate that system in a limited way.
Some other examples would be bash as the scripting language of unix, python as the scripting language of the sublime text editor, elisp as the scripting language of emacs, lua as the scripting language for World of Warcraft, etc.
When we say a function is 'built-in', we mean it is actually a function of the hosting environment (e.g. web-browser), not a function of the scripting language (JavaScript).
Although the JavaScript standard mandates certain built in functions, all that means is that a conforming host environment needs to expose that functionality, regardless of what language the underlying implementation is in.