@@ -12,44 +12,44 @@ come costruttore.
1212Se la funzione che è stata chiamata non ha un'istruzione ` return ` esplicita,
1313allora essa ritorna implicitamente il valore di ` this ` (il nuovo oggetto).
1414
15- function Foo( ) {
16- this.bla = 1 ;
15+ function Person(name ) {
16+ this.name = name ;
1717 }
1818
19- Foo .prototype.test = function() {
20- console.log(this.bla );
19+ Person .prototype.logName = function() {
20+ console.log(this.name );
2121 };
2222
23- var test = new Foo ();
23+ var sean = new Person ();
2424
25- Questo esempio chiama ` Foo ` come costruttore ed imposta il ` prototype ` del
26- nuovo oggetto creato a ` Foo .prototype` .
25+ Questo esempio chiama ` Person ` come costruttore ed imposta il ` prototype ` del
26+ nuovo oggetto creato a ` Person .prototype` .
2727
2828In caso di istruzione ` return ` esplicita, la funzione ritorna il valore
2929specificato da quell'istruzione, ma ** solo** se il valore di ritorno è un
3030` Object ` .
3131
32- function Bar () {
33- return 2 ;
32+ function Car () {
33+ return 'ford' ;
3434 }
35- new Bar (); // un nuovo oggetto
35+ new Car (); // un nuovo oggetto, non 'ford'
3636
37- function Test () {
38- this.value = 2;
37+ function Person () {
38+ this.someValue = 2;
3939
4040 return {
41- foo: 1
41+ name: 'Charles'
4242 };
4343 }
44- new Test(); // l'oggetto ritornato
44+ new Test(); // l'oggetto ritornato ({name: 'Charles'}), escluso someValue
4545
4646Quando la parola chiave ` new ` viene omessa, la funzione ** non** ritornerà un
4747nuovo oggetto.
4848
49- function Foo () {
50- this.bla = 1 ; // imposta la proprietà dell 'oggetto globale
49+ function Pirate () {
50+ this.hasEyePatch = true ; // imposta la proprietà nell 'oggetto globale!
5151 }
52- Foo (); // undefined
52+ var somePirate = Pirate (); // somePirate è undefined
5353
5454Mentre l'esempio precedente potrebbe sembrare essere funzionante in alcuni
5555casi, a causa del modo in cui lavora [ ` this ` ] ( #function.this ) in JavaScript,
@@ -60,27 +60,27 @@ esso userà l'*oggetto globale* come valore di `this`.
6060Per poter omettere la parola chiave ` new ` , la funzione costruttore deve
6161esplicitamente ritornare un valore.
6262
63- function Bar () {
64- var value = 1 ;
63+ function Robot () {
64+ var color = 'gray' ;
6565 return {
66- method : function() {
67- return value ;
66+ getColor : function() {
67+ return color ;
6868 }
6969 }
7070 }
71- Bar .prototype = {
72- foo : function() {}
71+ Robot .prototype = {
72+ someFunction : function() {}
7373 };
7474
75- new Bar ();
76- Bar ();
75+ new Robot ();
76+ Robot ();
7777
78- Entrambe le chiamate a ` Bar ` ritornano lo stesso risultato, un nuovo oggetto
78+ Entrambe le chiamate a ` Robot ` ritornano lo stesso risultato, un nuovo oggetto
7979creato con una proprietà chiamata ` method ` , che è una [ Closure] ( #function.closures ) .
8080
81- Bisogna anche notare che la chiamata ` new Bar () ` ** non** influisce sul prototipo
81+ Bisogna anche notare che la chiamata ` new Robot () ` ** non** influisce sul prototipo
8282dell'oggetto ritornato. Mentre il prototipo sarà impostato con il nuovo oggetto
83- creato, ` Bar ` non ritornerà mai quel nuovo oggetto.
83+ creato, ` Robot ` non ritornerà mai quel nuovo oggetto.
8484
8585Nell'esempio sopra, non c'è differenza funzionale nell'usare o meno la parola
8686chiave ` new ` .
@@ -93,19 +93,20 @@ può portare a bug potenzialmente insidiosi da risolvere.
9393Per poter creare un nuovo oggetto, si dovrebbe invece usare una factory e
9494costruire un nuovo oggetto all'interno di quella factory.
9595
96- function Foo () {
97- var obj = {};
98- obj.value = 'blub ';
96+ function CarFactory () {
97+ var car = {};
98+ car.owner = 'nobody ';
9999
100- var private = 2;
101- obj.someMethod = function(value) {
102- this.value = value;
100+ var milesPerGallon = 2;
101+
102+ car.setOwner = function(newOwner) {
103+ this.owner = newOwner;
103104 }
104105
105- obj.getPrivate = function() {
106- return private ;
106+ car.getMPG = function() {
107+ return milesPerGallon ;
107108 }
108- return obj ;
109+ return car ;
109110 }
110111
111112Sebbene questo esempio sia a prova di omissione della parola chiave ` new ` e
@@ -126,4 +127,3 @@ bug, **non** è certo un motivo per privarsi completamente dell'uso dei prototip
126127Alla fine si tratta di decidere quale sia la soluzione più adatta per
127128l'applicazione. È ; specialmente importante scegliere uno specifico stile
128129di creazione degli oggetti ed usarlo in maniera ** consistente** .
129-
0 commit comments