Thursday, 7 May 2020

JavaScript and How It Is Not Much Related to Java

JavaScript was created in 1995 at the beginning of Internet era. It is called JavaScript because it was first created using all Java keywords, but the 2 languages are totally different, one is threaded, one is event-based.

JavaScript has evolved much, and with ES6 (ECMAScript6), JavaScript comes with threading, and classes too.

Example class in ES6 (the file name must be .mjs):
class some_class {
  Some_Property = null;
  
  some_function(Some_Param){
  }

  static some_static_function(Some_Param){
  }

  async some_function2(Some_Param){
  }

  static async some_static_function2(Some_Param){
  }
}

//Static block for static properties
{
  some_class.Some_Static_Prop = null;
}

//ES6 export
export default some_class;
//EOF

Import class to use:
//Static import
import some_class from "./some_class.mjs";

//Dynamic import
(async function(){
  var some_class = await import("./some_class.mjs").default;
})();

Run Node.js with ES6 and multi-threading:
node --experimental-modules --experimental-worker \
app.js

Add the 'require' function to ES6:
npm install mjs-require --save
node --experimental-modules --experimental-worker -r mjs-require \
app.js

Run Node.js ES6 with heredoc in Bash:
node --experimental-modules --experimental-worker \
--input-type module <<'HEREDOC'
  console.log(Date.now());
HEREDOC

No comments:

Post a Comment