Typescript - Spontaneous Anonymous Functions

How can I create standalone executable anonymous functions using a script type?

for instance

(function() { var someClass = { } }.call(this)); 

I want to create a plugin that can work for Node.js, also for fron-tend.

+5
source share
4 answers
 /** * Self executing anonymous function using TS. */ (()=> { // Whatever is here will be executed as soon as the script is loaded. console.log('executed') })(); 

I want to create a plugin that can work for Node.js, also for fron-tend.

In this case, you must compile your TypeScript into AMD and use the AMD loader on the interface, for example http://requirejs.org/docs/start.html

On the server side, you will need to use the requirejs node package to download the file. Take a look at this: http://requirejs.org/docs/node.html

Basically, there are two ways to compile TS for JS using AMD compatible with the browser, or using CommonJS, which corresponds to Node.js. Downloading an AMD script in a browser or server requires the use of an AMD compatible bootloader, and * requirejs ** is one of them. (most famous / used, I would say)

+19
source

The first rule in TypeScript: Any valid JavaScript is valid TypeScript.

No, there is no particular way to write self-completion of anonymous functions in TS at the moment.

But below is some code that can be used in your situation.

Each class in TS is compiled to (named) spontaneous anonymous functions that returns a function.

Example:

 //ts class someClass { someProperty = "this is a property"; } 

Translates to

 //js var someClass = (function () { function someClass() { this.someProperty = "this is a property"; } return someClass; })(); 

Hope this helps.

+2
source

Self-executing, immediate, recursive top-level Re-level function in Typescript:

 (function handleAutomatonTypeChange(newtype: AutomatonType) { ReactDOM.render( <Automaton automatonType={newtype} onAutomatonTypeChange={handleAutomatonTypeChange} />, document.getElementById('automaton') ); })('diadic'); 
0
source
 (function(){ document.body.innerHTML = "Self Calling Function"; }.call(this)); 
-1
source

All Articles