web base-05-js-object
TRANSCRIPT
Corso Data Journalist gen-mar 2017
TECNOLOGIE WEB BASE
Oggetti
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)}, }
Costruzione di un oggetto
tramite notazione letterale
Medesima sintassi di utilizzo attributi e metodiBOY[‘AGE’] oppure BOY.AGE
BOY[‘SayHello’]() oppure BOY.SayHello()
tramite costruttore Object o {}
Costruzione di un oggetto tramite function
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
La parola chiave this
nella notazione letteralevar 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)};
nel costruttore Object / {} BOY = { 'AGE':12, 'HEIGHT': 1.45, ‘SayHello': function(){console.log(this.AGE)}, ‘Say':function(W){cvaronsole.log(W,W,W)},
}
in un oggetto costruito tramite function (crea un prototipo di oggetto)
function BOY() { this.AGE=12; this.HEIGHT= 1.45;}
prototipo di oggetti
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; }
Costruzione Utilizzo
Risultato
MF { firstName: 'John', lastName: 'Doe', age: 50, eyeColor: 'blue' }
MM { firstName: 'Sally', lastName: 'Rally', age: 48, eyeColor: 'green' }
sintassi a catena
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;}; }
funzione Boyattributi
metodi
oggetti MYSON1 MYSON2
var MYSON1=new Boy('John','Smith','male',11);var MYSON2=MYSON1;
MYSON2.setName('Paul') .setAge(8) ;
Oggetti Javascript e python
Javascript Pythonclass 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
LEZIONE 1..www.fordatascientist.orgD00-Web-JsBaseD14-Nodejs-Object.ipynb