Source code of implementation JavaScript internal methods
Asked Answered
A

1

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?
Arturoartus answered 16/4, 2016 at 0:6 Comment(3)
It's browser dependent. Also some browsers might not even use JavaScript for its internal calculations.Necktie
For example of I want to see with chrome?Arturoartus
See also How to see the source of Built-in javascript functions?Holliman
R
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


Rudolph answered 16/4, 2016 at 0:24 Comment(20)
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?Arturoartus
@Arturoartus Remember to upvote answers that help you, and mark an answer as the answer if you think it answered your question!Rudolph
@Arturoartus I'm not sure I understand what you're asking in the comment. Could you rephrase the question?Rudolph
If for example I see the code behind scrollTop () method it is normal javascript? could someone reproduce it with normal javascript?Arturoartus
Yes, it's just regular javascript. It's the exact same as writing the method yourself and placing it in <script> tags in your .HTML files.Rudolph
I am trying to reproduce ScrollTop method but I have no ideas?Do you know how this method is implemented?Arturoartus
@MatthewCliatt: Your answer is misleading. Chrome is a slightly tweaked Chromium, which is entirely open source.Galer
@Galer Thanks, I'm about to edit my answer to represent this fact. I'll also do some research and add what I find.Rudolph
@Arturoartus Have you searched the mozilla or chromium docs for this function?Rudolph
@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.Galer
Yes but I haven't found it:(Arturoartus
@Galer 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.Rudolph
@Arturoartus I'll have a look and see what I can find for it.Rudolph
You're trying to reproduce scrollTop, why not just use it?Rudolph
@Arturoartus It looks like scrollTop isn't a Javascript method, rather it's simply an attribute of HTML elements. Read this page on Element.scrollTop.Rudolph
So it seems that javascript method just show us the value of this attribute.It is like padding or margin...no?Arturoartus
@Arturoartus 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 .scrollTopRudolph
And what is the difference between scrolltop property and margin of an element:)I am a little bit confused:):)Arturoartus
@Arturoartus You can change scrollTop, and it will affect the scrolling distance. You can not change margin.Rudolph
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?Arturoartus

© 2022 - 2024 — McMap. All rights reserved.