Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 24, 2021 02:43 am GMT

typescript

letconst JavaScript letvar JavaScript

constletin

TypeScript JavaScript letconst. var.

JavaScriptvarJavaScript

var

JavaScript var

var a = 10;

avalue10


function f() {  var message = "Hello, world!";  return message;}


function f() {  var a = 10;  return function g() {    var b = a + 1;    return b;  };}var g = f();g(); // returns '11'

gaf. g aainfgfa.

function f() {  var a = 1;  a = 2;  var b = g();  a = 3;  return b;  function g() {    return a;  }}f(); // returns '2'

var

function f(shouldInitialize: boolean) {  if (shouldInitialize) {    var x = 10;  }  return x;}f(true); // returns '10'f(false); // returns 'undefined'

xifvar - - var-scopingfunction-scoping


function sumMatrix(matrix: number[][]) {  var sum = 0;  for (var i = 0; i < matrix.length; i++) {    var currentRow = matrix[i];    for (var i = 0; i < currentRow.length; i++) {      sum += currentRow[i];    }  }  return sum;}

JavaScript forii


for (var i = 0; i < 10; i++) {  setTimeout(function () {    console.log(i);  }, 100 * i);}

setTimeout


10101010101010101010

setTimeouti

setTimeoutforfori1010

IIFE - -i

for (var i = 0; i < 10; i++) {  // capture the current state of 'i'  // by invoking a function with its current value  (function (i) {    setTimeout(function () {      console.log(i);    }, 100 * i);  })(i);}

iifor

let

varletletvar

let hello = "Hello!";

using letvarfor-loop

function f(input: boolean) {  let a = 100;  if (input) {    // Still okay to reference 'a'    let b = a + 1;    return b;  }  // Error: 'b' doesn't exist here  return b;}

ab afwhilebif

catch

try {  throw "oh no!";} catch (e) {  console.log("Oh well.");}// Error: 'e' doesn't exist hereconsole.log(e);

let TypeScript

a++; // illegal to use 'a' before it's declared;let a;

ES2015 TypeScript

function foo() {  // okay to capture 'a'  return a;}// illegal call 'foo' before 'a' is declared// runtimes should throw an error herefoo();let a;

Mozilla

var

function f(x) {  var x;  var x;  if (true) {    var x;  }}

x xlet

let x = 10;let x = 20; // error: can't re-declare 'x' in the same scope

TypeScript

function f(x) {  let x = 100; // error: interferes with parameter declaration}function g() {  let x = 100;  var x = 100; // error: can't have both declarations of 'x'}


function f(condition, x) {  if (condition) {    let x = 100;    return x;  }  return x;}f(false, 0); // returns '0'f(true, 0); // returns '100'

shadowingsumMatrixlet

function sumMatrix(matrix: number[][]) {  let sum = 0;  for (let i = 0; i < matrix.length; i++) {    var currentRow = matrix[i];    for (let i = 0; i < currentRow.length; i++) {      sum += currentRow[i];    }  }  return sum;}

ii

var

function theCityThatAlwaysSleeps() {  let getCity;  if (true) {    let city = "Seattle";    getCity = function () {      return city;    };  }  return getCity();}

cityif

setTimeout IIFE for TypeScript

let IIFE setTimeoutlet

for (let i = 0; i < 10; i++) {  setTimeout(function () {    console.log(i);  }, 100 * i);}


0123456789

const

const

const numLivesForCat = 9;

let let


const numLivesForCat = 9;const kitty = {  name: "Aurora",  numLives: numLivesForCat,};// Errorkitty = {  name: "Danielle",  numLives: numLivesForCat,};// all "okay"kitty.name = "Rory";kitty.name = "Kitty";kitty.name = "Cat";kitty.numLives--;

TypeScript ECMAScript 2015 Mozilla



let input = [1, 2];let [first, second] = input;console.log(first); // outputs 1console.log(second); // outputs 2

first second

first = input[0];second = input[1];


// swap variables[first, second] = [second, first];


function f([first, second]: [number, number]) {  console.log(first);  console.log(second);}f([1, 2]);

...

let [first, ...rest] = [1, 2, 3, 4];console.log(first); // outputs 1console.log(rest); // outputs [ 2, 3, 4 ]

JavaScript

let [first] = [1, 2, 3, 4];console.log(first); // outputs 1


let [, second, , fourth] = [1, 2, 3, 4];console.log(second); // outputs 2console.log(fourth); // outputs 4


let tuple: [number, string, boolean] = [7, "hello", true];let [a, b, c] = tuple; // a: number, b: string, c: boolean


let [a, b, c, d] = tuple; // Error, no element at index 3

...,

let [a, ...bc] = tuple; // bc: [string, boolean]let [a, b, c, ...d] = tuple; // d: [], the empty tuple


let [a] = tuple; // a: numberlet [, b] = tuple; // b: string


let o = {  a: "foo",  b: 12,  c: "bar",};let { a, b } = o;

abfromo.ao.bc


({ a, b } = { a: "baz", b: 101 });

JavaScript a {

...

let { a, ...passthrough } = o;let total = passthrough.b + passthrough.c.length;



let { a: newName1, b: newName2 } = o;

a: newName1anewName1

let newName1 = o.a;let newName2 = o.b;


let { a, b }: { a: string; b: number } = o;

function keepWholeObject(wholeObject: { a: string; b?: number }) {
let { a, b = 1001 } = wholeObject;
}
b?bundefined keepWholeObject forwholeObjectaand bb


type C = { a: string; b?: number };function f({ a, b }: C): void {  // ...}


function f({ a = "", b = 0 } = {}): void {  // ...}f();

Cb

function f({ a, b = 0 } = { a: "" }): void {  // ...}f({ a: "yes" }); // ok, default b = 0f(); // ok, default to { a: "" }, which then defaults b = 0f({}); // error, 'a' is required if you supply an argument


let first = [1, 2];let second = [3, 4];let bothPlus = [0, ...first, ...second, 5];

bothPlus [0, 1, 2, 3, 4, 5]firstsecond


let defaults = { food: "spicy", price: "$$", ambiance: "noisy" };let search = { ...defaults, food: "rich" };

search{ food: "rich", price: "$$", ambiance: "noisy" }

let defaults = { food: "spicy", price: "$$", ambiance: "noisy" };let search = { food: "rich", ...defaults };

overwrites fooddefaultsfood: "rich"


class C {  p = 12;  m() {}}let c = new C();let clone = { ...c };clone.p; // okclone.m(); // error!

TypeScript


Original Link: https://dev.to/kennana/typescript-5636

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To