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.

201 lines
3.1 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

---
gitea: none
include_toc: true
---
[TOC]
# Structures de données natives
<mark>TODO</mark>
+ 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
```javascript
// [JS] JavaScript
function foo(arg1, arg2 = "2") {
//...
return 2.3;
}
foo(1)
```
```python
# [🐍] Python
def foo(arg1, arg2 = "2", /): # les paramètres positionnel uniquement
# ...
return 2.3
foo(1)
```
```typescript
//[TS] TypeScript
function foo(arg1: number, arg2?: string = "2"): number {
// ...
return 2.3;
}
foo(1)
```
## Paramètre du reste (rest parameter)
```javascript
// [JS] JavaScript
function foo(arg1, ...arg2) {
// arg2 = ["2", "3"]
return 2.3;
}
foo(1, "2", "3")
```
```python
# [🐍] Python
def foo(arg1, /, *arg2):
# arg2 = ("2", "3")
return 2.3
foo(1, "2", "3")
```
```typescript
//[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
```javascript
// [JS] JavaScript
function foo(arg1, arg2, arg3) {
//...
return 2.3;
}
const args = ["2", "3"];
foo(1, ...args)
```
```python
# [🐍] Python
def foo(arg1, arg2, arg3, /):
# ...
return 2.3
args = ("2", "3")
foo(1, *args )
```
```typescript
//[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
<mark>TODO: utilité</mark>
```javascript
// [JS] JavaScript
function * foo(nb) {
for(let i = 0; i < nb; ++i)
yield i;
}
for( let val of foo(5) )
console.log(val);
```
```python
# [🐍] Python
def foo(nb, /) {
for i in range(nb):
yield i;
}
for val in foo(5)
console.log(val);
```
```typescript
//[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
<mark>TODO</mark>: pas d'équivalent en python + utilité (callbacks)
```javascript
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