10

Is there a way to see the code behind a JavaScript's method? Not a javascript method from the website's .html or .js files, but JavaScript's internal methods.

For example:

  • How can I see how JavaScript calculates the offsetTop of an element?
Matt C
  • 4,470
  • 5
  • 26
  • 44
Fistright
  • 175
  • 1
  • 11

1 Answers1

6

JavaScript is implemented by the browser, so it depends on the browser.

Google's browser, Chrome, is closed-source not open-source. Which means that you can't view their source code, including their implementation of JavaScript. But, Chrome's source code is based on Chromium's source code, which is open source. You can view all of its source code in its git repository here. See more about this Chrome-Chromium relationship at the bottom of my answer.

Mozilla's browser, Firefox, is open-source just like all of their projects. You can view all of the source code for Mozilla projects here. You'll find the code that implements JavaScript in Firefox right here.

For closed-source implementations of JavaScript, like Chrome's, you can never be sure exactly how each method works. By reading the documentation available (see below), you will be able to get the best available idea of how a method might be implemented.

Note that just because Chrome's source code is based off of the open source project, Chromium, that doesn't mean the source code is the same. Chrome could have made tweaks to to JavaScript methods, and we wouldn't know. I think that is unlikely though, and all of the differences between Chrome and Chromium are most likely listed on this wikipedia page, and a nice post is available here on AskUbuntu

You can learn a lot more about Chromium's source code here.

Chrome JS documentation


Matt C
  • 4,470
  • 5
  • 26
  • 44
  • 1
    Thank you so much:):)An other quastion:the code behind javascript method is equal to normal code of javascript or it has a different implementation? – Fistright Apr 16 '16 at 00:27
  • @Fistright Remember to upvote answers that help you, and mark an answer as `the answer` if you think it answered your question! – Matt C Apr 16 '16 at 00:31
  • @Fistright I'm not sure I understand what you're asking in the comment. Could you rephrase the question? – Matt C Apr 16 '16 at 00:31
  • If for example I see the code behind scrollTop () method it is normal javascript? could someone reproduce it with normal javascript? – Fistright Apr 16 '16 at 00:34
  • Yes, it's just regular javascript. It's the exact same as writing the method yourself and placing it in ` – Matt C Apr 16 '16 at 00:41
  • I am trying to reproduce ScrollTop method but I have no ideas?Do you know how this method is implemented? – Fistright Apr 16 '16 at 00:43
  • 2
    @MatthewCliatt: Your answer is misleading. Chrome is a slightly tweaked Chromium, which is [entirely open source](https://www.chromium.org/developers/how-tos/get-the-code). – Blender Apr 16 '16 at 00:49
  • @Blender Thanks, I'm about to edit my answer to represent this fact. I'll also do some research and add what I find. – Matt C Apr 16 '16 at 00:57
  • @Fistright Have you searched the mozilla or chromium docs for this function? – Matt C Apr 16 '16 at 01:13
  • @MatthewCliatt: Can you name a single difference between how Chrome and Chromium render pages or execute JavaScript? I don't think it's fair to say that Chrome "could" be doing something different just because it's closed source. – Blender Apr 16 '16 at 01:33
  • Yes but I haven't found it:( – Fistright Apr 16 '16 at 01:33
  • @Blender I don't think there is a single difference. And I state that by saying that I think it is unlikely Chrome has changed any javascript code from Chromium. But I can't without hard evidence, say that it is ***absolutely certain*** that Chrome hasn't changed any javascript code from the Chromium project. – Matt C Apr 16 '16 at 01:38
  • @Fistright I'll have a look and see what I can find for it. – Matt C Apr 16 '16 at 01:38
  • You're trying to reproduce `scrollTop`, why not just use it? – Matt C Apr 16 '16 at 02:01
  • @Fistright It looks like `scrollTop` isn't a Javascript method, rather it's simply an attribute of HTML elements. Read [this page on Element.scrollTop](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop). – Matt C Apr 16 '16 at 02:04
  • So it seems that javascript method just show us the value of this attribute.It is like padding or margin...no? – Fistright Apr 16 '16 at 02:17
  • @Fistright What JavaScript method? There isn't a JavaScript method for scrollTop. But, just like any attribute on an html element, you can access and modify it in JavaScript code with `.scrollTop` – Matt C Apr 16 '16 at 02:23
  • And what is the difference between scrolltop property and margin of an element:)I am a little bit confused:):) – Fistright Apr 16 '16 at 02:25
  • @Fistright You can change scrollTop, and it will affect the scrolling distance. You can not change margin. – Matt C Apr 16 '16 at 03:12
  • So scroll top is a property of an element and JavaScript doesn't calcolate it but allow us to show it and modify it...right? – Fistright Apr 16 '16 at 19:51