Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see the source of built-in JavaScript functions? [closed]

E.g., alert(), Object(), String(), etc. How would I see the code behind these functions?

Would I need to understand the language that an engine, such as V8, is written in and read through that, or is there a simpler solution?

like image 580
jFasaJr Avatar asked Mar 10 '14 12:03

jFasaJr


People also ask

Where is JavaScript source code?

Go to the outer edge of a site (e.g. far left), then right-click and click on “View Page Source” (or the option that's similarly named). That will bring up all the HTML code, along with links to CSS files, Javascript, images, etc.

Where are functions stored in JavaScript?

#JavaScript Functions are Objects! They are not objects, don't have methods and they are stored in memory by value. Non-Primitives (functions, arrays and objects): these are mutable data types. They are objects and they are stored in memory by reference.

Which variables are declared in JavaScript when the page is closed?

Global variables live until the page is discarded, like when you navigate to another page or close the window. Local variables have short lives. They are created when the function is invoked, and deleted when the function is finished.

How do I check if a JavaScript function is running?

Use the typeof operator to check if a function is defined, e.g. typeof myFunction === 'function' . The typeof operator returns a string that indicates the type of a value. If the function is not defined, the typeof operator returns "undefined" and doesn't throw an error.


1 Answers

How would I see the code behind these functions?

You would need to find the source code for the relevant project (Firefox, Chromium, V8, SpiderMonkey, etc.), provided the project is open-source.

Would I need to understand the language that an engine such as V8 is written in and read through that, or is there a simpler solution.

Yes, you would. Note that alert is not a JavaScript function, it's a function defined by web browsers (so you'd need to look at the Firefox or Chromium, etc., source). Object and String are both part of JavaScript, so you'd look at the JavaScript engine sources for those (SpiderMonkey [or whateverMonkey, the prefix seems to change a lot] and V8, etc.).

like image 186
T.J. Crowder Avatar answered Oct 19 '22 09:10

T.J. Crowder