2

Suppose I want to call some JavaScript function. I should keep the required resource file (the JavaScript file) in the application folder and link. But what are the built-in functions like eval(), toString() are generated from? In particular:

  1. Are these functions retrieved from JavaScript files or any other mechanism?
  2. If so, where are they located?
  3. Are these subpart of browser installation?
Rajaram Shelar
  • 7,537
  • 24
  • 66
  • 107
  • 2
    yes they are part of browser. – A.T. Jul 03 '13 at 05:21
  • Even though the answer may seem obvious to experienced guys like me, I think this is a good question, +1 – user123444555621 Jul 03 '13 at 05:32
  • I wish you could give comments negative votes. No, this is not a good question. The user understands what a *built-in* function is, but not anything else about them? Are you to ask the same question for any reserved *keyword* in any language? It's one thing to ask the scoping of these built-ins, but another to ask where they are stored. Where is the HTML interpreter? Where is the CSS interpreter? How do `mailto:` links know what mail client to open? --- these are all basic questions that could probably be answered with basic Yahoo/Bing/Google. – vol7ron Jul 03 '13 at 06:02
  • @vol7ron Agree with your comment, this is very basic question, but we should know what happen at behind the scene while developing web application and while using the resource files – Rajaram Shelar Jul 03 '13 at 06:31
  • @vol7ron As noted in a comment below, the question is helpful to C programmers who have gotten used to `#include ` and wonder why this is not required in JS. Note that the term "built-in" is commonly used for standard functions that are not necessarily part of the language. – user123444555621 Jul 03 '13 at 07:07
  • @Pumbaa80 I still think it's a *basic* question. I do feel the eraj should investigate it, if he doesn't know - knowledge is a good thing - but that it's really not the type of question you expect on Stack Overflow, it's more of a SuperUser question. Regardless, if a programmer doesn't understand this, they may want to look up the difference between a compiled language and a scripting language. Also note, after reading my previous comment, I was joking about the negative comment votes - that may not have been clear given the content of the rest of the comment. – vol7ron Jul 03 '13 at 13:51

3 Answers3

6

1.Are these functions retrieved from js files or any other mechanism?

No, built-in functions are part of the language and most likely implemented in C or C++. However, since JavaScript is a dynamic language, a built in function could be re-defined somewhere by a Javascript function. For example:

String.prototype.substr = function () { return 'Take that, built-in function!'; };
var s = 'Hello';
window.alert(s.substr(1,2));

2.If yes where are they located?

See answer 1. However, with open-source JavaScript engines you'd be able to dig up the source code online if you were curious about the implementation of these built in functions. For example, the source code to V8, the JavaScript engine Chrome uses, can be found here.

One way to tell if a function is native would be to pop it up in an alert:

window.alert(Math.floor);

This will give you an alert box saying something like:

enter image description here

...indicating the code is native and cannot be displayed as JavaScript.

3.Are these subpart of browser installation?

They are part of the JavaScript installation which ships with the browser. Different browsers have different JavaScript engines.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • Yes agree. But for fetching the functions we have to reference them. But in javascript are not referencing anything – Rajaram Shelar Jul 03 '13 at 05:29
  • @eraj - Correct. They are *built-in* to the language, defined by the ECMA standard. There's no need to reference anything. – Mike Christensen Jul 03 '13 at 05:31
  • 1
    This is different to languages like C, where you'd have to explicitly [reference](http://stackoverflow.com/questions/6723266/why-to-include-stdlib-h) the standard library: `#include `. Oh boy, what a nuisance that is. I love JS! – user123444555621 Jul 03 '13 at 06:04
  • 1
    @Pumbaa80: that is only for standard input/output functions; whereas there are keywords (eg datatypes) that I think are part of the language and not loaded in ant specific library. These *built-in* functions would be similar to those reserved words. – vol7ron Jul 03 '13 at 06:11
0

Browsers implement a certain standard set of functions to comply with the Ecma standard. This means that you can trust that certain functionality will be offered out of the box

TGH
  • 38,769
  • 12
  • 102
  • 135
0

have you ever enabed/disabled javascript in browser, if no go to settings of your browser and see there, that means yes the script is shipped with browser.

javascript comes with all browser to support interactivity of web UI and other functions.

check this link for some comparison of javascript engines

A.T.
  • 24,694
  • 8
  • 47
  • 65