web base - javascript (node.js): elementi di base
TRANSCRIPT
![Page 1: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/1.jpg)
TECNOLOGIE WEB BASE
Javascript (Node.js):elementi di base
![Page 2: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/2.jpg)
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
![Page 3: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/3.jpg)
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.
![Page 4: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/4.jpg)
var MESSMESS= 'Hello World!';
VARIABILI
JavascriptAssegnazione
PythonAssegnazione
MESS=’Hello World’
dichiarazione
![Page 5: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/5.jpg)
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)
![Page 6: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/6.jpg)
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)
![Page 7: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/7.jpg)
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)
![Page 8: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/8.jpg)
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)
![Page 9: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/9.jpg)
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";
![Page 10: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/10.jpg)
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)
![Page 11: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/11.jpg)
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
![Page 12: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/12.jpg)
TECNOLOGIE WEB BASE
Javascript e Node.js:oggetti globali
![Page 13: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/13.jpg)
Oggetti globali javascript e Node.js
javascript Node.jsconsole console
E’ un oggetto globale che fornisce una interfaccia di debug.
console.log(‘......’) console.log(‘.....’)Scrive la stringa passata come argomento nello standard output
L’oggetto console di Node.js non replica perfettamente l’oggetto console dei principali browser. Ad esempio non ha implementato il metodo .clean() per cancellare il contenuto dello standard output
![Page 14: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/14.jpg)
Moduli base Node.js e python
Node.js Pythonos sys path process
osvar os=require('os');var sys=require('sys');var path=require('path');
import osimport sys
sys
process.env.PATH sys.path
path.dirname()path.join('...','...’')process.cwd();os.hostname()os.arch()process.pid
os.path.curdiros.sys.join(‘......’, ‘.......’)os.path.abspath(os.path.curdir)os.uname()
os.getpid()
![Page 15: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/15.jpg)
LEZIONE 1..www.fordatascientist.org
D00-Web-JsBaseD10-Nodejs-Introduction
![Page 16: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/16.jpg)
TECNOLOGIE WEB BASE
Funzioni
![Page 17: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/17.jpg)
Funzioni: tramite dichiarazione
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);
![Page 18: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/18.jpg)
Funzioni: tramite assegnazione
Javascriptvar Sum= function (N1,N2){
// This function does sum of two numberconsole.log('Sum is running')S=N1+N2;return S
}
var N;N=Sum(12,74);
dal punto di vista funzionale, la creazione di una funzione tramite dichiarazione è equivalente alla creazione tramite assegnazione. La creazione tramite assegnazione può essere utile in termini di “chiarezza del codice” quando si vuole utilizzare la funzione come input di un’altra funzione. Ad esempio
var SAY=function(WORD) {console.log(WORD);}
function Exec(someF, VAL) {someF(VAL);}
Exec(SAY, "Hello");
![Page 19: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/19.jpg)
Funzioni: dichiarazione e assegnazione
Le funzioni create tramite dichiarazione sono caricate in memoria centrale prima di processare le altre istruzioni.
Una funzione creata tramite dichiarazione può essere posizionata anche dopo la sua chiamata.
![Page 20: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/20.jpg)
Scope delle variabili interne ad una funzione
Javascript Python
possono essere sia locali che globali. Sono locali se sono create tramite il costruttore ‘var’. Sono globali se sono create tramite una semplice assegnazione.
una variabile creata all’interno di una funzione è sempre locale, a meno che essa prima di essere creata non sia stata definita come ‘global’
![Page 21: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/21.jpg)
LEZIONE 1..www.fordatascientist.org
D00-Web-JsBaseD12-Nodejs-Function
![Page 23: Web base - Javascript (Node.js): Elementi di base](https://reader031.vdocumenti.com/reader031/viewer/2022021500/587547ea1a28abb8208b6959/html5/thumbnails/23.jpg)
varnumber
typeof
string
object
[ ]
{ }constructor