You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3.1 KiB

Table of Contents

[TOC]

Structures de données natives

TODO

  • types de bases / array / object / Map / Set + conversions + Date / RegExp

  • == vs ===

  • Math API

  • Manipuler URL => move 2 JS-DOM API

  • class + getter/setter + private/protected + interface + iterator / async inter / héritage + fonctionnement prototype (méthode => fct et inversement + bind)

    • ts enum
  • commentaires / const / readonly / frozen

  • console/assert

  • opérateurs (vs Python

  • déstructuration / décomposition / gabarit / coalescence des nuls / chaînage opt

      • dans les fonctions
  • Exceptions

  • structures conditionnelles

  • imports

  • fct async (#30)

  • fct génératrices

  • Deno API

Fonctions

Déclaration

// [JS] JavaScript
function foo(arg1, arg2 = "2") {
    //...
    return 2.3;
}
foo(1)
# [🐍] Python
def foo(arg1, arg2 = "2", /): # les paramètres positionnel uniquement
    # ...
    return 2.3

foo(1)
//[TS] TypeScript
function foo(arg1: number, arg2?: string = "2"): number {
    // ...
    return 2.3;
}

foo(1)

Paramètre du reste (rest parameter)

// [JS] JavaScript
function foo(arg1, ...arg2) {
    // arg2 = ["2", "3"]
    return 2.3;
}
foo(1, "2", "3")
# [🐍] Python
def foo(arg1, /, *arg2):
    # arg2 = ("2", "3")
    return 2.3

foo(1, "2", "3")
//[TS] TypeScript
function foo(arg1: number, ...arg2: readonly string[]): number {
    // arg2 = ["2", "3"]
    return 2.3;
}

foo(1, "2", "3")

Opérateur de décomposition (spread operator) comme argument

// [JS] JavaScript
function foo(arg1, arg2, arg3) {
    //...
    return 2.3;
}
const args = ["2", "3"];
foo(1, ...args)
# [🐍] Python
def foo(arg1, arg2, arg3, /):
    # ...
    return 2.3

args = ("2", "3")
foo(1, *args )
//[TS] TypeScript
function foo(arg1: number, arg2: string, arg3: string): number {
    return 2.3;
}
const args = ["2", "3"] as const; // ou as ["string", "string"]
foo(1, ...args)

Fonctions génératrices

TODO: utilité

// [JS] JavaScript
function * foo(nb) {
    for(let i = 0; i < nb; ++i)
        yield i;
}

for( let val of foo(5) )
    console.log(val);
# [🐍] Python
def foo(nb, /) {
    for i in range(nb):
        yield i;
}

for val in foo(5)
    console.log(val);
//[TS] TypeScript
function * foo(nb: number): Generator<number> {
    for(let i = 0; i < nb; ++i)
        yield i;
}

for(let val of foo(5) )
    console.log(val);

Fonctions fléchées

TODO: pas d'équivalent en python + utilité (callbacks)

const foo = arg => arg[0]; // est équivalent à :
const foo = function(arg){ return arg[0]; }

// si plusieurs paramètres, utiliser des parenthèses ().
// si valeur retournée plus complexe, utiliser des accolades {}.
const foo = (arg1, args) => { ... ; return x; } // est équivalent à :
const foo = function(arg1, arg2){ ... ; return x; }

Asynchronisme

/!\ Diff with Python

  • Promise

  • motivation + pas bloquer thread principal