Friday, November 15, 2024

JavaScript - The Definitive Guide 7th Edition

 JavaScript - The Definitive Guide 7th Edition

1. ES6 was released in 2015 and added major new features - including class and module syntax. Since ES6, the EMCAScript has moved to a yearly release ES2016, ES2017 ...

2. Install Node and run node snippet.js

    Console.log("Hello World");

or hello.html <script src="hello.js"></script>

3. let book = {

    topic: "JavaScript",

    edition: 7

};

book.topic

book["edition"] //another way to access property values

book.author="Flanagan";//create new properties

book.contents?.ch01?.set //conditionally access (ES2020)

4. In ES6 and laster, arrow function

    const plus1 = x=>x+1;

5. Semicolons are optional

6. In ES6 and later, string literals can be delimited with backticks:

let name = "Bill";

let greeting = `Hello ${name}`;

7. In ES6 and later use let and const

8. a??b->(A!==null&&a!==undefined)?a:b

9. let a = [1,2,3]

 let b = [0,...a,4];

10. let data = [1,2,3,4,5], sum  = 0;

data.forEach(value=>{sum+=value;});

data.forEach(function(v,i,a){a[i]=v+1;}); //data=[2,3,4,5,6]

11. let a = [1,2,3];

a.map(x=>x*x) //[1,4,9]

12. let a = [1,2,3,4,5];

a.reduce( (x,y)=>x+y,0) //15, sum

a.reduce( (x,y)=>X*y,1) //120, product

the second argument is optional, init value to the funciton

13.

[1,[2,3]].flat() // [1,2,3]

[1,[2,[3]].flat() //[1,2,[3]]

14. a.flatMap(f)=>a.map(f).flat()

let phrases = ["Hello world","The Guide"];

let words = phrases.flatMap( phrases=>phrases.split(" ")); //["Hello","world","The","Guide"]

15. let a = [1,2,3];

a.join(" ") //"1 2 3"

16. f.call(o);

      f.apply(o);

To invoke a function f as a method of the object o. apply() is like call, except that the arguments to be passed to the function are specified as an array: f.apply(0,[1,2]);

17. In Node, each file is an independent module with a private namespace.

Node modules import other modules with the require() function.

const fs = require("fs");

18. ES6 Exports

export const PI = Math.PI;

export function degreesToRadias(d){return d*PI/180;}

ES6 Imports

import BitSet from './bitset.js';

19. Promise (from ES6)

getJSON("/api/user/profile").then(displayUserProfile,handleProfileError);

if getJSON runs normally, it pases its result to displayUserProfile

if there is an error, then it passes an Error object to handleProfileError

getJSON("/api/user/profile").then(displayUserProfile).catch(handleProfileError);

any error in getJSON or in displayUserProfile, get passed to handleProfileError

queryDatabase().catch(e=>wait(500).then(queryDatabase))

                       .then(displayTable)

                        .catch(displayDatabaseError);

20.

let [value1,value2]=await Promise.all(getJSON(url1),getJSON(url2)];

21.

If you don't want your node program to completely crash.

    process.setUncaughtExceptionCaptureCallback( error=>{

  appLogger.error("Uncaught Exception: ",error);
});

process.on("unhandledRejection",(reason,promise)=>{
  appLogger.error("unhandledRejection: ",reason);
});

No comments:

Post a Comment