Corso avanzato di Tecnologie WEB - Funzioni e oggetti
Click here to load reader
Transcript of Corso avanzato di Tecnologie WEB - Funzioni e oggetti
var SUM= function(N1,N2){// This function does sum of two numberconsole.log('Sum is running')S=N1+N2;return S
}
function SUM(N1,N2){// This function does sum of two numberconsole.log('Sum is running')S=N1+N2;return S
}
Medesima chiamataSUM(12,34)
In un programma è possibile inserire la chiamata della funzione prima della sua
costruzione
E’ possibile utilizzare una funzione come parametro di un’altra funzione
Obiettivo: vogliamo costruire una funzione Add che lanciata in qualunque punto del programma incrementi di uno il contatore COUNT
Soluzione debole (variabile globale COUNT)
var COUNT= 0;
function Add() { COUNT=COUNT+1 }
Il problema: tutti i programmi possono modificare COUNT;
Soluzione sbagliata (variabile locale COUNT)
function Add() { var COUNT= 0;
COUNT=COUNT+1 }
Il problema: tutte le volte che eseguo Add(), COUNT assume il valore 1;
Soluzione ottima (funzione closure): costruisco una funzione che si auto-invoca, con una variabile locale COUNT e che restituisce una funzione che incrementa
COUNT
var Add = ( function () { var CONT = 0; return function () {return COUNT=COUNT+1;}
}) ( );
dopo l’esecuzione del programma:COUNT è una variabile locale di Add()Add()={return COUNT=COUNT+1;}
var SUM = new Function('N1', 'N2', 'S=N1 + N2; return S');
Stessa chiamata degli altri metodi di costruzioneSUM(12,34)
var BOY= new Object()var BOY= {}
BOY.AGE=10;BOY['HEIGHT']=1.20;
BOY.SayHello= function(){console.log('Hello')};BOY.Say=function(W){console.log(W,W,W)};
var BOY = { 'AGE':12, 'HEIGHT': 1.45, NAME: 'Jonh', 'SayHello': function(){console.log('Hello')}, 'Say': function(W){console.log(W,W,W)}, }
Medesima sintassi di utilizzo attributi e metodiBOY[‘AGE’] oppure BOY.AGE
BOY[‘SayHello’]() oppure BOY.SayHello()
function BOY() {}BOY.AGE=12;BOY.HEIGHT= 1.45;BOY.NAME= ‘Jonh’;BOY.SayHello= function(){console.log('Hello')};BOY.Say=function(W){console.log(W,W,W)};
function BOY() {AGE=12;HEIGHT= 1.45;NAME= 'Jonh';SayHello= function(){console.log('Hello')};Say=function(W){console.log(W,W,W)}; }
function BOY() {AGE:12,HEIGHT: 1.45,NAME: 'Jonh',SayHello: function(){console.log('Hello')},Say:function(W){console.log(W,W,W)}, }
ERROREALTRO
SIGNIFICATO
var BOY= new Object()var BOY= {}
BOY.AGE=10;BOY.HEIGHT=1.20;BOY.SayHello= function(){console.log(this.AGE)};BOY.Say=function(W){console.log(W,W,W)};
var BOY = { 'AGE':12, 'HEIGHT': 1.45, ‘SayHello': function(){console.log(this.AGE)}, ‘Say':function(W){console.log(W,W,W)},
}
function BOY() { this.AGE=12; this.HEIGHT= 1.45;}
var MF = new Person("John", "Doe", 50, "blue");var MM = new Person("Sally","Rally", 48, "green");
function Person(first, last, age, eye) {this.firstName = first;this.lastName = last;this.age = age;this.eyeColor = eye; }
MF { firstName: 'John', lastName: 'Doe', age: 50, eyeColor: 'blue' }
MM { firstName: 'Sally', lastName: 'Rally', age: 48, eyeColor: 'green' }
function Boy(NAME, SURNAME, GENDER, AGE) { this.name = NAME; this.surname = SURNAME; this.gender = GENDER; this.age = AGE; this.setName = function(x) {this.name = x; return this;}; this.setSurname = function(x) {this.surname = x; return this;}; this.setAge = function(x) {this.age= x; return this;}; this.setGender = function(x) {this.gender = x; return this;}; }
attributi
metodi
var MYSON1=new Boy('John','Smith','male',11);var MYSON2=MYSON1;
MYSON2.setName('Paul') .setAge(8) ;
class UNO():def __init__(self,X,Y):
self.X=X self.Y=Y
def Show(self): print('Questa è la variabile X: ',self.X) print('Questa è la variabile Y: ',self.Y)
A=UNO(10,20)
A.Show()
Questa è la variabile X: 10Questa è la variabile Y: 20
function UNO(X,Y) {this.X=X;this.Y=Y;this.Show=function(){
console.log('Questa è la variabile X: ',this.X); console.log('Questa è la variabile Y: ',this.Y); }}
var A =new UNO(10,20);
A.Show()
Questa è la variabile X: 10Questa è la variabile Y: 20