Rajaram Shelar
Rajaram Shelar

Reputation: 7877

How javascript function are loaded in the script tag

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?

Upvotes: 2

Views: 106

Answers (3)

Mike Christensen
Mike Christensen

Reputation: 91628

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.

Upvotes: 6

TGH
TGH

Reputation: 39258

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

Upvotes: 0

A.T.
A.T.

Reputation: 26312

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

Upvotes: 0

Related Questions