0

My question is setTimeout function and other web ApIs written in another programming language ? how javascript uses the other programming language features because other programming language syntax is different. Is there any translator between them which converts the code of another language in javascript ?.

10
  • It's really hard to tell what you're asking. Syntax is not the same thing as the standard library, and even that differs between JavaScript runtimes (browsers, Node.js, Rhino, ...) Commented May 19, 2020 at 10:52
  • setTimeout is not a part of JavaScript. That's implemented by browser's interpreters, say v8 Engine. JavaScript supports only sequential code execution. You might like to read event-loop in JS as well. Commented May 19, 2020 at 10:57
  • 1
    @RahulDwivedi - V8 doesn't implement setTimeout, the host does. (V8 does have a setTimeout placeholder -- you can see this if you run V8 without any host -- but it doesn't actually do a timer, it just calls the function immediately.) Commented May 19, 2020 at 10:58
  • 1
    @T.J.Crowder Thanks for clarifying in details :) (y) Commented May 19, 2020 at 11:02
  • 1
    @RahulDwivedi - Indeed, the ES2015 spec where promises were added was the first edition of the specification to define any asynchronous behavior. :-) Commented May 19, 2020 at 11:22

2 Answers 2

2

There's no language translation involved, no, although there's definitely a boundary between the environments.

When a browser uses a JavaScript engine, the browser provides some things to the engine in order for the engine to do its work. One of those things is the global object, which has methods and properties on it that provide host-specific features (like the DOM, setTimeout, etc.). The browser also provides functions to the JavaScript engine that aren't exposed to JavaScript code, for doing things like resolving modules.

Think of the JavaScript engine as a library embedded in the browser application. The browser code calls into the library to do things like create a new environment for a window/tab, and provides functions to the library that it calls to do things like schedule timer callbacks.

Sign up to request clarification or add additional context in comments.

Comments

0

Those functions exposed from the browser.

Basically it goes like

  • lex the js

  • do proper actions (wait for the timeout for instance)

  • push the callback back to js stack (possibly with extra data from the result of the native function)

about your second question whether there are any "transpilers" transpiling oter languages to js:

There are plenty of transpilers the simplest example is tsc which transpiles typescript to javascript

Also you didnt ask but I feel like you want to learn how things work so I will add a bonus.

WebAssembly is a binary format for the browser and it is a compiler target for many languages which means without bothering to transpile things to js you can simply comile your native program to webassembly and run native code in your browser

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.