Corso base di Tecnologie WEB - Primi passi in javascript
Transcript of Corso base di Tecnologie WEB - Primi passi in javascript
TECNOLOGIE WEB BASE
Primi passi in javascript con Node.js
Programmi sincroni e asincroni
Programmi sincroniUna istruzione viene
eseguita solo dopo che è terminata l’esecuzione
dell’istruzione precedente.
Programmi asincroniUna istruzione può essere
eseguita prima che sia terminata l’esecuzione
dell’istruzione precedente.
PYTHON
JAVASCRIPTNella interrelazione con il DOM o con un server
Javascript e Node.js
Node.js è un versione di javascript che può essere eseguita dal server.
Per fare i primi passi in javascript, utilizzeremo la versione node.js eseguita da server che, non interagendo con il dom, non pone problemi di
esecuzione asincrona delle istruzioni.
var MESSMESS= 'Hello World!';
VARIABILI
JavascriptAssegnazione
PythonAssegnazione
MESS=’Hello World’
dichiarazione
STRINGHE
Javascript(string/ ‘’)
Python(str)
var STR, STR1, STR2;STR1=’Hello ’;STR2=’world’;STR =STR1+STR2;
STR.replace(‘Hello’,’Wonderfull’);
STR.lenght
typeof STRSTR.constructor()
STR1=’Hello ’;STR2=’world’;STR =STR1+STR2;
STR.replace(‘Hello’,’Wonderfull’);
len(STR)
type(STR)
Numeri
Javascript(number/ 0)
Python(int, float)
var NUM1, NUM2, NUM3;NUM1=5;NUM2=3.66;NUM3=NUM1/NUM2;
NUM3.toFixed()
NUM3.toString()
typeof NUM3NUM3.constructor()
NUM1=5;NUM2=3.66;NUM3=NUM1/NUM2;
int(round(NUM3,0))
str(NUM3)
type(NUM3)
Liste
Javascript(object/ [ ])
Python(list)
var LIS1, LIS2;
LIS1=[10,12,52,64,37,'a','b']LIS2=[7,55,44]
LIS1+LIS2 stringLIS1*2 NaNLIS1.concat(LIS2)LIS1[0]LIS1[5]LIS1.slice(0,3)
typeof LIS1LIS1.constructor()
LIS1=[10,12,52,64,37,'a','b']LIS2=[7,55,44]
LIS1+LIS2 concatenazioneLIS1*2 LIS1+LIS1LIS1+LIS2LIS1[0]LIS1[5]LIS1[0:3]
type(LIS1)type(LIS1)
Dizionari
Javascript(object/ { })
Python(dict)
var DIZ1, DIZ2;
DIZ1={'DEU':100, 'FRA':150 }DIZ2={'ITA':50, 'ESP':500}
DIZ1[‘DEU’]
typeof DIZ1DIZ1.constructor()
DIZ1={'DEU':100, 'FRA':150 }DIZ2={'ITA':50, 'ESP':500}
DIZ1[‘DEU’]
type(DIZ1)
Blocco if
Javascript Python
var V1=20;var RIS='';
if (V1<5) {RIS="V1 is <=5";}else if (V1<15) {RIS="V1 is ranging 5-15";}else {RIS="V1 is >=15";}
V1=20
if V1<5: RIS="V1 is <=5"elif V1<15: RIS="V1 is ranging 5-15"else: RIS="V1 is >=15";
Ciclo for
Javascript Python
var text=''for (var i=0; i<10; i=i+1) {text = text +" " + i}
text=''for i in range(0,10):
text = text +" " + str(i)
Ciclo while
Javascript Python
var i=0;var text=''while (i < 10) {text = text +" " + i; i=i+1;}
text=''i=0while True:
text = text +" " + str(i)i=i+1if i >= 10:
break
LEZIONE 1..www.fordatascientist.orgD00-Web-JsBase
D10-Nodejs-Introduction.ipynb
varnumber
typeof
string
object
[ ]
{ }constructor
Funzioni
Javascript Python
function Sum(N1,N2){// This function does sum of two numberconsole.log('Sum is running')S=N1+N2;return S
}
var N;N=Sum(12,74);
def Sum(N1,N2):‘’’This function does sum of two number’’’print('Sum is running')S=N1+N2;return S
N=Sum(12,74);