Professional Documents
Culture Documents
Pour Débutant
J AVA S C R I P T (Programmation Internet) V O L . I I
INTRODUCTION :
1. Commentaire multiligne :
/*
lignes de commentaires */
2. Commentaires en ligne : :
2.b.
<!-- Commentaire en ligne
2.c.
-> commentaire en ligne
Attention :
En fait « --> » n’est pas un opérateur comme tel, mais simplement le com-
plexe [ opérateur de décrémentation « -- » espacé de son opérande (de
gauche) et suivi directement du signe supérieur à « > » ] donnant « -- > » :
await, abstract, arguments, boolean, break, byte, case, catch, char, class,
const, continue, debugger, default, delete, do, double, else, enum, eval, ex-
port, extends, false, final, finally, float, for, function, goto, if, implements,
import, in, instanceof, int, interface, let, long, NaN, native, new, null, pack-
age, private, protected, public, return, short, static, super, switch, synchro-
nized, this, throw, throws, transient, true, try, typeof, var, void, volatile,
while, with, yield.
<html>
<head>
<script type="text/javascript" language="JavaScript">
...
</script>
</head>
<body>
...Éléments HTML...
<script language="JavaScript" type="text/javascript">
...
</script>
</body>
Toutes les balises ci-dessus sont recommandées, mais pour un tout petit
script, seul l’élément SCRIPT est indispensable.
Rappelons que nous avons utilisé ici l’instruction « alert » pour l’affichage.
<div id='id'></div>
document.getElementById('id').innerText+=
"document.getElementById('id').innerText ";
document.getElementById('id').innerHTML+=
"document.getElementById('id').innerHTML";
console.log("console.log");
console.dir("console.dir");
alert("alert")
print("print")
</script>
<script language="JavaScript"
type="text/javascript" src=URL>
</script>
<script language="JavaScript"
type="text/javascript"
src="myOwnSubdirectory/myOwn2ndJavaScript.js">
</script>
URL est l’emplacement du fichier de script qui peut être local à votre poste
de travail ou dans un réseau (LAN ou WEB) selon la syntaxe :
src="http://www.server.com/scriptfile.js"
ou
src="directory/scriptfile.js">
Une variable peut être déclarée ou non, initialisée (valeur par défaut) ou pas
(« undefined »)
Un paramètre formel peut être déclaré ou non, initialisé (avec une valeur par
défaut ou implicite) ou pas (« undefined »).
console.log(arithm(10, 2)); // 20
console.log(arithm(20)); // 100
</script>
Exécution :
23:24:31,287 20 test.html:7:3
23:24:31,289 100 test.html:8:3
Un paramètre par défaut est celui qui est TOUJOURS pris en considération
avec la valeur lui définie quand la fonction appelante ne lui a pas envoyé
d’argument correspondant, mais prend éventuellement la valeur de
l’argument lui envoyé par la fonction appelante.
console.log(ard(Math.PI)) // 3.14
console.log(ard(Math.PI,2)) // 3.14
console.log(ard(Math.PI,0)) // 3
console.log(ard(Math.PI,-1)) // 0
console.log(ard(Math.PI,5)) // 3.14159
console.log((Math.PI).toFixed(5)) // 3.14159
console.log(ard(Math.PI,Math.E)) // 3.1411691558523307
Variables & Functions - 11 / 99 - lundi, 11. mars 2019 (7:17 )
J.D.B. DIASOLUKA Nz. Luyalu JavaScript Tome-II
console.log((Math.PI).toFixed(Math.E)) // 3.14
</script>
<script type="text/javascript">
E = Math.E; // Le mode strict déclaré plus loin
///////// function f(p=5) { "use strict";
// SyntaxError: "use strict" not allowed
// in function with default parameter
// test.html:3:20
<script type="text/javascript">
E = Math.E; // mode strict déclaré après
function f(p=(a)=>{with(i in a){
console.log(i+"VRAI DUMMY")}
}) {
const PI=Math.PI;
console.log(E+" & "+PI+" & **"+p+"**")
}
f(); // Argument écrase paramètre par défaut.
Opera 12 :
Ligne: 2
Car: 18
Erreur: ')' attendu
Code: 0
URL: file:///K:/test.html
console.log(arithm(10, 2)); // 20
console.log(arithm(20)); // 100
console.log(arithm(20, undefined)); // 100
console.log(arithm(20, null)); // 0
</script>
Exécution :
23:28:01,876 20 test.html:5:3
23:28:01,878 100 test.html:6:3
08:30:04,521 100 test.html:7:3
08:30:04,521 0 test.html:8:3
08:39:28,460 20 test.html:5:3
08:39:28,463 NaN test.html:6:3
08:39:28,463 NaN test.html:7:3
08:39:28,463 0 test.html:8:3
var v
<script>
let l ;
alert(l) ;
var v ;
alert(v) ;
const c ;
alert(c) ;
</script>
Firefox :
Yandex :
<script>
v=new Date() ;
Variables & Functions - 16 / 99 - lundi, 11. mars 2019 (7:17 )
J.D.B. DIASOLUKA Nz. Luyalu JavaScript Tome-II
alert(v)
</script>
Avec Firefox :
Avec Yandex :
Avec document.write() :
"use strict";
<script type="text/javascript">
v=25;
function fct(){
// Déclaration/définition de fonction
v='...série d\'instructions...';
console.log("v="+v);
// v=...série d'instructions
var v=100;
// déclarée locale, APRÈS avoir été utilisée.
}
La déclaration var :
En réalité le mot-clé var sert à déclarer une variable comme locale c’est-
à-dire « visible / accessible seulement dans le scope de sa déclaration ».
console.log(a , window.a);
// colobome colobome
f();
</script>
<script>
function f() {
var v=new Date() ;
}
alert(v)
// Uncaught ReferenceError: v is not defined
</script>
<script>
function f() {
var v=new Date() ;
}
f();
alert(v);
</script>
Exécution:
<script>
function f() { v=new Date("11,Aug,1953") ; }
f(); // Appelée APRÈS définition.
<script type="text/javascript">
j=11, m = -25, a=1953;
function fext(){
j=3957; // Jusqu'ici j est globale, = 11.
m=8; // m globale écrase -25 -> 8
var a=2017; // Déf variable a=2017 locale.
var j; // Force j en locale malgré déjà uti-
lisée.
function fint(){
var m=new Date(); // m locale à fint() = Date.
console.log("* Dans fint()")
console.log("m="+m+" j="+j+" a="+a)
} // Fin function fext(){
fint() ;
fext();
// j et a = globales, m = modifiée ds fext(),
Les blocs ordinaires n’ont pas de portée limitée. À la sortie d’une boucle,
les variables locales « var » du bloc de la boucle persistent :
<script type="text/javascript">
j=11, m = -25, a=1953;
for(k=0;k<5;k++){
var m=new Date(); // m locale à fimbriquee().
}
console.log("k="+k+" , m = "+m.toLocaleString());
// k=5 , m = 23/12/2017 à 02:12:19
</script>
Autre exemple :
<script>
for (k = 0; k <= 5; k++){
var f = function() {
return function(x){return k + x;};
}
if (k == 0) {fct1 = f(); };
if (k == 5) {fct2 = f(); };
}
console.log(fct1(3)); // 9
console.log(fct2(6)); // 12
</script>
En mode sloppy :
<script type="text/javascript">
d=Date(); console.log(d);
// Sat Feb 24 2018 22:21:20 GMT+0100 (Paris, Madrid)
</script>
let l;
let l;
/* FIERFOX : SyntaxError:
redeclaration of let l test.html:6:5
note: Previously declared at line 5, column 5
YANDEX :
Uncaught SyntaxError:
Identifier 'l' has already been declared */
</script>
Les valeurs des propriétés d’un objet const peuvent cependant être mo-
difiées :
obj.id = 'wille';
console.log(obj); // Object { id: "wille" }
</script>
Dans les deux modes (sloppy et strict), une variable ou une expression
de fonction ne peuvent être déclarées qu’avec l’un des trois mots clés
suivants : const, let ou var, avec les spécificités suivantes :
console.log(C);
// Array [ "A", "B" ]
arr[0]= ["autre","array"] ;
arr[1] = {'X':'x' , 'Z':'z'};
console.dir(C);
console.dir(arr);
// […] , 0: "Zéro" , 1: "Un" , 2: "spl2" , 3: "PUSHÉ"
// length: 4
function fct(p){
p[0]="Zéro", p[1]="Un";
return p;
}
</script>
Note :
https://www.ecma-international.org/ecma-262/8.0/
Variables & Functions - 29 / 99 - lundi, 11. mars 2019 (7:17 )
J.D.B. DIASOLUKA Nz. Luyalu JavaScript Tome-II
6.1.7.1 Property Attributes
Attributes are used in this specification to define and explain the state of Object proper-
ties. A data property associates a key value with the attributes listed in Table 2.
Table 2: Attributes Value Domain Description
of a Data Property
Attribute Name
[[Value]] Any ECMAScript The value retrieved by a get access of the
language type property.
[[Writable]] Boolean If false, attempts by ECMAScript code to
change the property's [[Value]] attribute
using [[Set]] will not succeed.
[[Enumerable]] Boolean If true, the property will be enumerated by a
for-in enumeration (see 13.7.5). Otherwise,
the property is said to be non-enumerable.
[[Configurable]] Boolean If false, attempts to delete the property,
change the property to be an accessor prop-
erty, or change its attributes (other than
[[Value]], or changing [[Writable]] to false)
will fail.
Illustration :
<script>
let ob={3:"Cataracte" , glc:"Glaucome" ,
"arr":["Cataracte" , "Glaucome"]};
console.log(
Object.getOwnPropertyDescriptors(ob)
);
/* Affiche
Object { 3: {…}, glc: {…}, arr: {…} }
__proto__:Object
*/
console.log(
Object.getOwnPropertyDescriptors(ob[3])
);
/*
Ob-ject { 0: {…}, 1: {…}, 2: {…}, 3: {…}, 4: {…}, 5: {…},
6: {…}, 7: {…}, 8: {…}, length: {…} }
console.log(
Object.getOwnPropertyDescriptors(ob["3"])
);
/*
Ob-ject { 0: {…}, 1: {…}, 2: {…}, 3: {…}, 4: {…}, 5: {…},
6: {…}, 7: {…}, 8: {…}, length: {…} }
__proto__:Object
Variables & Functions - 32 / 99 - lundi, 11. mars 2019 (7:17 )
J.D.B. DIASOLUKA Nz. Luyalu JavaScript Tome-II
*/
console.log(
Object.getOwnPropertyDescriptors(ob.glc)
);
/*
Ob-ject { 0: {…}, 1: {…}, 2: {…}, 3: {…}, 4: {…}, 5: {…},
6: {…}, 7: {…}, length: {…} }
__proto__:Object
*/
console.log(
Object.getOwnPropertyDescriptors(ob["glc"])
);
/*
Ob-ject { 0: {…}, 1: {…}, 2: {…}, 3: {…}, 4: {…}, 5: {…},
6: {…}, 7: {…}, length: {…} }
__proto__:Object
*/
console.log(
Object.getOwnPropertyDescriptors(ob["arr"])
Variables & Functions - 34 / 99 - lundi, 11. mars 2019 (7:17 )
J.D.B. DIASOLUKA Nz. Luyalu JavaScript Tome-II
);
/*
Object { 0: {…}, 1: {…}, length: {…} }
__proto__:Object
*/
console.log(
Object.getOwnPropertyDescriptors(ob["arr"][0])
);
/*
Ob-ject { 0: {…}, 1: {…}, 2: {…}, 3: {…}, 4: {…}, 5: {…},
6: {…}, 7: {…}, 8: {…}, length: {…} }
__proto__:Object
*/
console.log(
Object.getOwnPropertyDescriptors(ob["arr"][1])
);
/*
Ob-ject { 0: {…}, 1: {…}, 2: {…}, 3: {…}, 4: {…}, 5: {…},
6: {…}, 7: {…}, length: {…} }
__proto__:Object
*/
</script>
An accessor property associates a key value with the attributes listed in Table 3.
Table 3: Value Domain Description
Attributes of an
Accessor Property
Attribute Name
[[Get]] Object | Undefined If the value is an Object it must be a func-
tion object. The function's [[Call]] internal
method (Table 6) is called with an empty
arguments list to retrieve the property
value each time a get access of the proper-
ty is performed.
[[Set]] Object | Undefined If the value is an Object it must be a func-
tion object. The function's [[Call]] internal
method (Table 6) is called with an argu-
ments list containing the assigned value as
its sole argument each time a set access of
the property is performed. The effect of a
property's [[Set]] internal method may, but
is not required to, have an effect on the
value returned by subsequent calls to the
property's [[Get]] internal method.
[[Enumerable]] Boolean If true, the property is to be enumerated by
a for-in enumeration (see 13.7.5). Other-
wise, the property is said to be non-
enumerable.
[[Configurable]] Boolean If false, attempts to delete the property,
change the property to be a data property,
If the initial values of a property's attributes are not explicitly specified by this specifi-
cation, the default value.
Table 4: Default Attribute Values Attri- Default Value
bute Name
[[Value]] undefined
[[Get]] undefined
[[Set]] undefined
[[Writable]] false
[[Enumerable]] false
[[Configurable]] false
// Via alias
var funcname=fct
funcname();
// Ds fct = fct(undefined,undefined)
// Avec eval()
function go3(p){
eval(p);
}
go3("fct(Math.E,Math.PI)")
// Ds fct =
fct(2.718281828459045,3.141592653589793)
//////////////////////////
// La fonction à appeler
//////////////////////////
function fct(){
console.log(
"Ds fct = " + fct.name +
"("+arguments[0]+","+arguments[1]+")"
);
}
</script>
p = "Subst1";
v = "ubst2";
f("un", "deux");
</script>
Une « variable locale static » est une variable définie dans une fonction
mais qui garde sa valeur même quand la fonction est quittée. JS ne com-
porte pas une telle sorte de donnée. Comment y suppléer ?
Mais dès qu’une fonction englobe une autre (fonction englobante) et re-
tourne cette dernière (fermeture ou closure) son environnement lexical n’est
plus détruit mais conservé à la fin de son exécution.
var n = f();
alert(n());
</script>
La fonction f() retourne un pointeur sur la fonction f2() qui elle, retourne la
valeur en cours de la variable b, qui est "b".
Du fait que la fonction f() est une fonction retournante, son environnement
n'est pas détruit à sa sortie, et toutes ses variables y compris les LOCALES
(déclarées var) sont préservées.
var n = f(); La variable « n » reçoit la valeur retournée par f() qui est
un pointeur sur la fonction interne retournée f2(). La fonction f2() est ce
qu’on appelle « closure ».
La variable b peut être modifiée, mais bien entendu, seulement par les fonc-
tions qui ont accès à elle, et durant la période de leur exécution.
<script type="text/javascript">
"use strict"; const cl=console.log;
function f(){
var b = 10;
cl(m());
cl(m());
cl(n());
cl(m());
cl(n());
</script>
Exécution :
for(let k=2;k<5;k++){
var r = fct(k * ((Math.random()*10) | 0));
// Partie entière seulement
console.log("y==(k*Math...)=="+r[0]+" | x=="+r[1]+
" | x+=y=="+(1*r[1]+r[0]));
}
</script>
Note:
<script type="text/javascript">
"use strict" ; const cl=console.log;
function genStatVar (a){
let statVar=a||10; // Par défaut =10.
cl(`*** statVar==${statVar}`);
return function fct(a,b){
if(a=="inc") return ++statVar;
else if(a=="dec") return --statVar;
else if(a=="exp") return stat-
Var=Math.pow(statVar,b);
else if(a=="root") return stat-
Var=Math.pow(statVar,1/b);
else if(a=="add") return statVar+=b;
else if(a=="sub") return statVar-=b;
else return "undefined parameter";
Variables & Functions - 43 / 99 - lundi, 11. mars 2019 (7:17 )
J.D.B. DIASOLUKA Nz. Luyalu JavaScript Tome-II
}
}
<script type="text/javascript">
"use strict" ; const cl=console.log;
// Inc
let incV = genStatVar("inc"); // *** statVar==10
cl(incV()); // 11
cl(incV()); // 12
// Exp , 2
let exp2 = genStatVar("exp",2); // *** statVar==2
cl(exp2(4)); // 16
cl(exp2(2)); // 256
// root , 1000000
let rootV = genStatVar("root",1000000); // *** stat-
Var==1000000
cl(rootV(2)); // 1000
cl(rootV(3)); // 9.999999999999998
// add , default
addV = genStatVar("add"); // *** statVar==10
cl(addV()); // 20
cl(addV()); // 40
cl(addV(-10)); // 30
// undefined
let dummy = genStatVar(); // *** statVar==10
cl(dummy()); // undefined parameter
cl(incV()); // 10
cl(exp2(2)); // 4294967296
cl(exp2_5(1)); // 1000000
</script>
Une autre façon de créer/gérer les variables locales statiques c’est via un
générateur.
function* createlocGenFiboGenerator() {
while (true) {
/**/
disp(locGenPrev,locGenFibo,locGenVar,1);
locGenVar = locGenPrev+locGenFibo;
/**/
disp(locGenPrev,locGenFibo,locGenVar,2);
locGenPrev=locGenFibo , locGenFibo=locGenVar;
yield [
(locGenFibo*2) + ". "+
1*(2000+Math.trunc(Math.random()*1000)),
"instant "+new Date().getTime()]
}
}
const ar=[
"Xérophtalmie","Entropion", "Rétinoblastome",
"Hyperphorie","Épiphora","Gérontoxon"
];
const l=ar.length;
const t=iterator.next().value;
console.dir(k+1);
const i=parseInt(t);
console.dir(t[1]);
console.dir(t[0] +". "+ar[k] + "***");
console.dir('\n'.padEnd(34,"#")+"\n\n");
}
</script>
Exécution :
13:15:22,868 1 test.html:83:4
13:15:22,875 instant 1542197722823 test.html:98:4
13:15:22,876 2. 2229. Xérophtalmie*** test.html:99:4
13:15:22,877 2 test.html:83:4
13:15:22,877 instant 1542197722878 test.html:98:4
13:15:22,877 4. 2409. Entropion*** test.html:99:4
13:15:22,890 3 test.html:83:4
13:15:22,891 instant 1542197722891 test.html:98:4
13:15:22,891 6. 2001. Rétinoblastome*** test.html:99:4
13:15:22,893 4 test.html:83:4
13:15:22,893 instant 1542197722893 test.html:98:4
13:15:22,894 10. 2259. Hyperphorie*** test.html:99:4
13:15:22,896 5 test.html:83:4
13:15:22,896 instant 1542197722896 test.html:98:4
13:15:22,899 16. 2903. Épiphora*** test.html:99:4
13:15:22,901 6 test.html:83:4
13:15:22,901 instant 1542197722901 test.html:98:4
13:15:22,901 26. 2117. Gérontoxon*** test.html:99:4
<script type="text/javascript">
date = new Date();
console.log(date.toLocaleDateString()); //
27/01/2018
console.log(date.toLocaleString()); //
27/01/2018 à 01:40:32
console.log(date.toUTCString()) ;// Sat, 27 Jan 2018
00:40:32 GMT
console.log(date.toGMTString()); // Sat, 27 Jan 2018
00:40:32 GMT
</script>
En mode strict :
2. Deux instructions sur une même ligne DOIVENT être séparées par un
point-virgule (semicolons), mais les instructions en fin de ligne ne
sont pas tenues de se terminer par le point-virgule (à cause de
l’Automatic Semicolon Insertion =ASI) vs au langage C, mais il est
préférable de les placer pour vous signaler la fin d’une instruction, un
k= 0. | Math.pow(k,k).toPrecision(10) = 1.000000000 =
(k**k).toExponential(10) =1.0000000000e+0
k= 1. | Math.pow(k,k).toPrecision(10) = 1.000000000 =
(k**k).toExponential(10) =1.0000000000e+0
k= 2. | Math.pow(k,k).toPrecision(10) = 4.000000000 =
(k**k).toExponential(10) =4.0000000000e+0
k= 3. | Math.pow(k,k).toPrecision(10) = 27.00000000 =
(k**k).toExponential(10) =2.7000000000e+1
k= 4. | Math.pow(k,k).toPrecision(10) = 256.0000000 =
(k**k).toExponential(10) =2.5600000000e+2
k= 5. | Math.pow(k,k).toPrecision(10) = 3125.000000 =
(k**k).toExponential(10) =3.1250000000e+3
k= 6. | Math.pow(k,k).toPrecision(10) = 46656.00000 =
(k**k).toExponential(10) =4.6656000000e+4
k= 8. | Math.pow(k,k).toPrecision(10) = 16777216.00 =
(k**k).toExponential(10) =1.6777216000e+7
k= 9. | Math.pow(k,k).toPrecision(10) = 387420489.0 =
(k**k).toExponential(10) =3.8742048900e+8
Note :
<script type="text/javascript">
"use strict"; const cl=console.log;
const fct = (a) => {cl(a) ; return a*a;}
cl(fct(5));
</script>
Exécution :
5 test.html:3:25
25 test.html:5:5
<script type="text/javascript">
"use strict"; const cl=console.log;
const fct = (a) => cl(a) , return a*a;
cl(fct(5));
</script>
Firefox 65.0
La virgule de chaînage peut aussi être utilisée dans les différentes parties
d’une boucle « for ». Dans l’exemple qui suit, notez la virgule de chaî-
nage entre « k = 0 » et « a.toLocaleSt... »
<script>
var a=new Date(9999,12,31)
Exécution :
<script>
const cl=console.log;
var a=Math.round(Math.random()*10) ,
b=Math.ceil(Math.log(a)) ,
c=0,d=0,e=0;
v = a<=7
?
a*a + b*b + 2*a*b
:
Variables & Functions - 58 / 99 - lundi, 11. mars 2019 (7:17 )
J.D.B. DIASOLUKA Nz. Luyalu JavaScript Tome-II
c = Math.pow(a,2) , d=b*b , e=2*a*b ;
cl(
` a=${a} _ b=${b} \n v=${v} \n`+
` c=${c} _ d=${d} _ e=${e}`
)
</script>
Exécutions :
1. Avec « a > 7 » :
2. Avec « a == 7 , <= 7 » :
Notez que
Variables & Functions - 59 / 99 - lundi, 11. mars 2019 (7:17 )
J.D.B. DIASOLUKA Nz. Luyalu JavaScript Tome-II
d=b*b , e=2*a*b ;
<script>
const cl=console.log;
var a=Math.round(Math.random()*10) ,
b=Math.ceil(Math.log(a)) ,
c=0,d=0,e=0;
let v = a<=7
?
a*a + b*b + 2*a*b
:
c = Math.pow(a,2) , d=b*b , e=2*a*b ;
cl(
` a=${a} _ b=${b} \n v=${v} \n`+
` c=${c} _ d=${d} _ e=${e}`
)
</script>
Boucle « while » :
Pour rembourrer une chaîne avec un caractère ou une autre chaîne donnée
(par défaut l’espace) pour faire une taille totale déterminée :
console.log('123'.padStart(16,"0"));
// Padding avant
// 0000000000000123
console.log('abc'.padEnd(16,"DIAS"));
// Padding après
// abcDIASDIASDIASD
</script>
console.log(Object.entries(data));
console.log("Object.keys(oArg)");
console.log(Object.keys(oArg));
console.log("");
console.log("Object.values(oArg)");
console.log(Object.values(oArg));
console.log("");
console.log("");
}
console.log(Object.keys(vl));
console.log( Object.values(vl));
Exécution :
12:39:53,259
Object.keys(oArg) ***
test.html:8:3
12:39:53,260
Array(3) [ "0", "1", "2" ]
0: "0"
1: "1"
2: "2"
length: 3
__proto__: Array(0)
test.html:9:3
12:39:53,261
Object.values(oArg) +++
test.html:13:3
12:39:53,262
Array(3) [ {…}, {…}, {…} ]
0: {Dadet: Array(2)}
1: {Pierrette: Array(4)}
2: {Jean: Array(3)}
length: 3
__proto__: Array(0)
test.html:14:3
12:39:53,265
*** [KEYS] *** Object.keys(oArg[k])
test.html:20:6
12:39:53,265
Array [ "Dadet" ]
0: "Dadet"
length: 1
__proto__: Array(0)
test.html:21:6
Variables & Functions - 64 / 99 - lundi, 11. mars 2019 (7:17 )
J.D.B. DIASOLUKA Nz. Luyalu JavaScript Tome-II
12:39:53,266
+++ [VALUES] *** Object.values(oArg[k])
test.html:23:6
12:39:53,266
Array [ (2) […] ]
[Array(2)]
0: (2) ["Homme", "Kinois"]
length: 1
__proto__: Array(0)
test.html:24:6
12:39:53,268
*** [KEYS] *** Object.keys(oArg[k])
test.html:20:6
12:39:53,268
Array [ "Pierrette" ]
0: "Pierrette"
length: 1
__proto__: Array(0)
test.html:21:6
12:39:53,268
+++ [VALUES] *** Object.values(oArg[k])
test.html:23:6
12:39:53,269
Array [ (4) […] ]
[Array(4)]
0: (4) ["Femme", "Alien", "Umo", "ExtraGalactic"]
length: 1
__proto__: Array(0)
test.html:24:6
12:39:53,270
Variables & Functions - 65 / 99 - lundi, 11. mars 2019 (7:17 )
J.D.B. DIASOLUKA Nz. Luyalu JavaScript Tome-II
*** [KEYS] *** Object.keys(oArg[k])
test.html:20:6
12:39:53,270
Array [ "Jean" ]
0: "Jean"
length: 1
__proto__: Array(0)
test.html:21:6
12:39:53,270
+++ [VALUES] *** Object.values(oArg[k])
test.html:23:6
12:39:53,271
Array [ (3) […] ]
[Array(3)]
0: (3) ["Homme", "Luozien", "Kinoinisé"]
length: 1
__proto__: Array(0)
test.html:24:6
13:34:27,116
Array [ "Dadet" ]
["Dadet"]0: "Dadet"
length: 1
__proto__: Array(0)
test.html:34:6
13:34:27,116
Array [ (2) […] ]
[Array(2)]
0: (2) ["Homme", "Kinois"]
length: 1
__proto__: Array(0)
test.html:35:6
13:34:27,117
Array [ (4) […] ]
0: (4) ["Femme", "Alien", "Umo", "ExtraGalactic"]
length: 1
__proto__: Array(0)
test.html:35:6
13:34:27,118
Array [ "Jean" ]
0: "Jean"
length: 1
__proto__: Array(0)
test.html:34:6
13:34:27,118
Array [ (3) […] ]
0: (3) ["Homme", "Luozien", "Kinoinisé"]
length: 1
__proto__: Array(0)
test.html:35:6
console.log(oyo);
with(oyo){
console.log(`${diamAP}, ${degMyopie}, ${refrCornée}`);
}
</script>
const j=retmultiple()
console.log(
"Auj = [" +
j[0].s + "], le " +
j[0].j + "/" + ++j[0].m + "/" + j[0].a +
" - " +
j[0].hr + ":" + j[0].mn + ":" + j[0].sc +
":" + j[0].ms
)
https:/www.ecma-international.org/ecma-262/8.0
Note :
There are two other special values, called positive Infinity and
negative Infinity. For brevity, these values are also referred to for
expository purposes by the symbols +∞ and -∞, respectively.
(Note that these two infinite Number values are produced by the
program expressions +Infinity (or simply Infinity) and -
Infinity.)
Note that there is both a positive zero and a negative zero. For
s × m × 2e
s × m × 2e
Note that all the positive and negative integers whose magnitude is
no greater than 253 are representable in the Number type (indeed,
the integer 0 has two representations, +0 and -0).
<script>
console.log("isNaN(NaN) ?" , isNaN(NaN));
console.log('isNaN("texte") ?' , isNaN("texte"));
console.log("isNaN('texte') ?" , isNaN("texte"));
console.log("isNaN('') ?" , isNaN(""));
console.log("isNaN(0) ?" , isNaN(0));
console.log("isNaN(-0) ?" , isNaN(-0));
console.log("isNaN(Number.POSITIVE_INFINITY) ?",
isNaN(Number.isNaNPOSITIVE_INFINITY));
console.log("isNaN(Number.NEGATIVE_INFINITY) ?",
isNaN(Number.isNaNNEGATIVE_INFINITY));
console.log("isNaN(3247) ?" , isNaN(3247));
console.log("isNaN(-34.68) ?" , isNaN(-34.68));
console.log("isNaN(false) ?" , isNaN(false));
</script>
Variables & Functions - 72 / 99 - lundi, 11. mars 2019 (7:17 )
J.D.B. DIASOLUKA Nz. Luyalu JavaScript Tome-II
N = M.E,
et que
Mais:
« (null == undefined) == true »,
« (null === undefined) == false ».
« (undefined==true) == false »
« (undefined==false) == false ».
var cudf;
var rcudf = 1 * cudf; // Donne NaN
et
« Object.is(true,true) == true »
« ( Object.is(true,true) == true ) == true »
« (Object.is(true,true) === true ) == true »
« Object.is(1,1) == true »
« ( Object.is(1,1) == true ) == true »
« ( Object.is(1,1) === true ) == true »
« Object.is(0,0) == true »
« ( Object.is(0,0) == true ) == true »
« ( Object.is(0,0) === true ) == true »
« Object.is(true,1) == false »
« ( Object.is(true,1) == true ) == false »
« ( Object.is(true,1) === true ) == false »
« ( Object.is(true,1) == false ) == true »
« ( Object.is(true,1) === false ) == true »
et
« Object . is ( -0 , 0 ) == false »
« (Object . is ( -0 , 0 ) == true ) == false »
« (Object . is ( -0 , 0 ) == false ) == true »
« (Object . is ( -0 , 0 ) === true ) == false »
« (Object . is ( -0 , 0 ) === false ) == true »
2. Méthode Number.isNaN() :
4. L’expression x != x :
x=0/0;
console.log(typeof x); // number
Les nombres en JavaScript sont saisis, dans les champs [de saisie], en for-
mat « littéral » (« String »). Leur addition fait l’addition concatenative et
non pas l’addition arithmétique.
CHAPITRE 7 : Le « TypeCasting » :
Et dans un code :
Mini Benchmark :
m = i;
console.log("Math.floor(m) = Math.floor(" + m + ") = " +
Math.floor(m));
console.log("");
console.log("");
var m = i;
console.log("parseInt(m) = " + "parseInt(" + m + ") = " +
parseInt(m));
console.log("");
m = i;
console.log("(m - (m % 1)) = (" + m + " - (" + m + " % 1))
= " +
(m - (m % 1)));
console.log(`${"\n".repeat(5)}${"*".repeat(30)}\n\n`);
Variables & Functions - 84 / 99 - lundi, 11. mars 2019 (7:17 )
J.D.B. DIASOLUKA Nz. Luyalu JavaScript Tome-II
m = i;
console.log("m | 0 = " + m + " | 0 = ",
m | 0);
(function () {
let b = null;
console.log("Parce que En DOUBLE-WORD (32 bits) :
");
console.log(m + "d = " + (b = m.toString(2)) +
"b");
console.log("10110010110100000101111000000000b
=");
console.log(
"1011'0010''1101'0000'''0101'1110''0000'0000b
= ",
m | 0 + "d");
console.log(
"Notez le bit (ou flag) du signe qui est à
1.");
}
)();
console.log("".padEnd(3, "\n"));
console.log("ES10 n'utilise pas encore le 64 bits : ");
m = new Uint8Array([3000000000.1]);
console.log("Uint8Array ([3000000000.1]) = " +
m.toString());
m = new Uint16Array([3000000000.1]);
console.log("Uint16Array ([3000000000.1]) = " +
Variables & Functions - 85 / 99 - lundi, 11. mars 2019 (7:17 )
J.D.B. DIASOLUKA Nz. Luyalu JavaScript Tome-II
m.toString());
m = new Uint32Array([3000000000.1]);
console.log("Uint32Array ([3000000000.1]) = " +
m.toString());
m = new Uint8ClampedArray([3000000000.1]);
console.log("Uint8ClampedArray([3000000000.1]) = " +
m.toString());
console.log("");
m = new Int8Array([3000000000.1]);
console.log("Int8Array ([3000000000.1]) = " +
m.toString());
m = new Int16Array([3000000000.1]);
console.log("Int16Array ([3000000000.1]) = " +
m.toString());
m = new Int32Array([3000000000.1]);
console.log("Int32Array ([3000000000.1]) = " +
m.toString());
console.log(`\n${"*".repeat(30)}${"\n".repeat(5)}` );
console.log("-----");
console.log("La suite ne retourne pas seulement la partie
entière");
console.log("-----");
console.log("");
m = i;
console.log("parseFloat(m) = parseFloat(" + m + ") = " +
parseFloat(m));
console.log("");
m = i;
console.log("m.toFixed(0) = " + m + ".toFixed(0) = " +
m.toFixed(0));
console.log("");
m = i;
console.log("Math.ceil(m) = Math.ceil(" + m + ") = " +
Math.ceil(m));
console.log("");
m = i;
console.log("Math.round(m) = Math.round(" + m + ") = " +
Math.round(m));
console.log("");
m = i;
console.log("m.toPrecision(5) = " + m + ".toPrecision(5) =
" +
m.toPrecision(5));
console.log("");
m = i;
console.log("m.toExponential(5) = " + m +
".toExponential(5) = " + m.toExponential(5));
console.log("");
m = i;
console.log("eval(m)= eval(" + m + ") = " + eval(m));
console.log("");
m = i;
console.log("Number(m) = Number("+m+") = "+Number(m));
console.log("");
m = i;
console.log("-(-m) = -("+-m+") = "+-(-m));
console.log("");
m = i;
console.log("1*m = 1*"+m+" = "+1*m);
console.log("");
m = i;
console.log("m/1 = "+m+"/1 = "+ m/1);
Exécution du Benchmark :
****************************** test.html:63:1
****************************** test.html:122:1
let t="";
for(let i in arr) t += arr[i] + " | ";
console.log(t);
</script>
16:03:15.217 test.html:6
83 | 40 | 12 | 89 | 78 | 12 | 10 | 73 | 78 | 77 |
Avec la boucle « for … in », on peut aussi dans certains cas parcourir toutes
les propriétés/attributs de certains éléments HTML/DOM comme « naviga-
tor », « window », « document » … sans manipuler d’indice :
console.log(
5 == num / 2 && !(3 + 2 * num).toString() === "");
// Affiche false.
QUIZ :
Exécution :
-2 || -2 == -2 /// -2 || -2 == -2
-2 || -1 == -2 /// -1 || -2 == -1
-2 || 0 == -2 /// 0 || -2 == -2
-2 || 1 == -2 /// 1 || -2 == 1
-2 || 2 == -2 /// 2 || -2 == 2
-1 || -2 == -1 /// -2 || -1 == -2
-1 || -1 == -1 /// -1 || -1 == -1
-1 || 0 == -1 /// 0 || -1 == -1
-1 || 1 == -1 /// 1 || -1 == 1
-1 || 2 == -1 /// 2 || -1 == 2
0 || -2 == -2 /// -2 || 0 == -2
1 || -2 == 1 /// -2 || 1 == -2
1 || -1 == 1 /// -1 || 1 == -1
1 || 0 == 1 /// 0 || 1 == 1
1 || 1 == 1 /// 1 || 1 == 1
1 || 2 == 1 /// 2 || 1 == 2
2 || -2 == 2 /// -2 || 2 == -2
2 || -1 == 2 /// -1 || 2 == -1
2 || 0 == 2 /// 0 || 2 == 2
2 || 1 == 2 /// 1 || 2 == 1
2 || 2 == 2 /// 2 || 2 == 2
Heureusement que depuis ES6 la définition des valeurs par défaut pour les
paramètres est possible (bien entendu incompatible avec les vieux browsers).
Voir paramètres par défaut (page 8).
Recommandation :
Pour tester un code HTML, DOM, JavaScript, CSS, allez sur l’IDE du
site http://jsfiddle.net/, dommage qu’il faut être online (connecté).
Mots-clés :
diasfb@mail2world.com
Autre Lecture :
https://www.scribd.com/document/374738470/Le-Plus-Grand-Secret-de-La-
Creation
• https://www.scribd.com/document/377036251/Le-Dosage-Des-
Medicaments-en-Cac-Cas
• https://www.scribd.com/document/377035454/Le-Hasard-Des-
Thermometres-Non-contact-a-Infrarouge
• https://www.scribd.com/document/376222482/Petite-Introduction-
Aux-Fonctions-JavaScript
• https://www.scribd.com/document/376221919/La-Foi-en-Jesus-Christ-
Pour-Quoi-Faire
• https://www.scribd.com/document/375689778/Lacuite-visuelle-
angulaire
• https://www.scribd.com/document/375349851/La-variable-This
• https://www.scribd.com/document/375024162/Fonctions-Imbriquees-
en-JS
• https://www.scribd.com/document/374789297/Format-Interne-Des-
Objets-JavaScript
• https://www.scribd.com/document/374788758/Iterations-en-JavaScript
• https://www.scribd.com/document/374738470/Le-Plus-Grand-Secret-
de-La-Creation
• https://www.scribd.com/document/374597969/Nouvelle-Formule-d-
IMC-indice-de-doduite-Selon-Dr-Diasoluka
• https://www.scribd.com/document/373847209/Property-Descriptors
• https://www.scribd.com/document/373833282/l-Objet-Global-Window
• https://www.scribd.com/document/372665249/Javascript-Tome-II
• https://www.scribd.com/document/355291488/motilite-oculaire-2
• https://www.scribd.com/document/355291239/motilite-oculaire-I
• https://www.scribd.com/document/355290248/Script-d-Analyses-Des-
Reflexes-Pupillomoteurs
• https://www.scribd.com/document/321168468/Renseignements-Id-et-
Anthropometriques
• https://www.scribd.com/document/320856721/Emission-31-Jul-2016
• https://www.scribd.com/document/318182982/Complication-Visuelle-
du-Traitement-de-La-Malaria
• https://www.scribd.com/document/318180637/Rapport-Entre-
Oxymetrie-Et-Type-Respiration
Variables & Functions - 98 / 99 - lundi, 11. mars 2019 (7:17 )
J.D.B. DIASOLUKA Nz. Luyalu JavaScript Tome-II
• https://www.scribd.com/document/315746265/Classification-Des-
Medicaments
• https://www.scribd.com/document/315745909/Incongruences-
Heresies-et-Heterodoxies-de-la-Notion-de-Laboratoire
• https://www.scribd.com/document/315745725/Rapport-Entre-
Oxymetrie-Et-Type-Respiration