@@ -11,74 +11,74 @@ constructor.
11
11
If the function that was called has no explicit ` return ` statement, then it
12
12
implicitly returns the value of ` this ` - the new object.
13
13
14
- function Foo( ) {
15
- this.bla = 1 ;
14
+ function Person(name ) {
15
+ this.name = name ;
16
16
}
17
17
18
- Foo .prototype.test = function() {
19
- console.log(this.bla );
18
+ Person .prototype.logName = function() {
19
+ console.log(this.name );
20
20
};
21
21
22
- var test = new Foo ();
22
+ var sean = new Person ();
23
23
24
- The above calls ` Foo ` as constructor and sets the ` prototype ` of the newly
25
- created object to ` Foo .prototype` .
24
+ The above calls ` Person ` as constructor and sets the ` prototype ` of the newly
25
+ created object to ` Person .prototype` .
26
26
27
27
In case of an explicit ` return ` statement, the function returns the value
28
28
specified by that statement, but ** only** if the return value is an ` Object ` .
29
29
30
- function Bar () {
31
- return 2 ;
30
+ function Car () {
31
+ return 'ford' ;
32
32
}
33
- new Bar (); // a new object
33
+ new Car (); // a new object, not 'ford'
34
34
35
- function Test () {
36
- this.value = 2;
35
+ function Person () {
36
+ this.someValue = 2;
37
37
38
38
return {
39
- foo: 1
39
+ name: 'Charles'
40
40
};
41
41
}
42
- new Test(); // the returned object
42
+ new Test(); // the returned object ({name:'Charles'}), not including someValue
43
43
44
44
When the ` new ` keyword is omitted, the function will ** not** return a new object.
45
45
46
- function Foo () {
47
- this.bla = 1 ; // gets set on the global object
46
+ function Pirate () {
47
+ this.hasEyePatch = true ; // gets set on the global object!
48
48
}
49
- Foo (); // undefined
49
+ var somePirate = Pirate (); // somePirate is undefined
50
50
51
- While the above example might still appear to work in some cases, due to the
52
- workings of [ ` this ` ] ( #function.this ) in JavaScript, it will use the
51
+ While the above example might still appear to work in some cases, due to the
52
+ workings of [ ` this ` ] ( #function.this ) in JavaScript, it will use the
53
53
* global object* as the value of ` this ` .
54
54
55
55
### Factories
56
56
57
57
In order to be able to omit the ` new ` keyword, the constructor function has to
58
58
explicitly return a value.
59
59
60
- function Bar () {
61
- var value = 1 ;
60
+ function Robot () {
61
+ var color = 'gray' ;
62
62
return {
63
- method : function() {
64
- return value ;
63
+ getColor : function() {
64
+ return color ;
65
65
}
66
66
}
67
67
}
68
- Bar .prototype = {
69
- foo : function() {}
68
+ Robot .prototype = {
69
+ someFunction : function() {}
70
70
};
71
71
72
- new Bar ();
73
- Bar ();
72
+ new Robot ();
73
+ Robot ();
74
74
75
- Both calls to ` Bar ` return the same thing, a newly created object that
75
+ Both calls to ` Robot ` return the same thing, a newly created object that
76
76
has a property called ` method ` , which is a
77
77
[ Closure] ( #function.closures ) .
78
78
79
- It should also be noted that the call ` new Bar () ` does ** not** affect the
79
+ It should also be noted that the call ` new Robot () ` does ** not** affect the
80
80
prototype of the returned object. While the prototype will be set on the newly
81
- created object, ` Bar ` never returns that new object.
81
+ created object, ` Robot ` never returns that new object.
82
82
83
83
In the above example, there is no functional difference between using and
84
84
not using the ` new ` keyword.
@@ -92,19 +92,21 @@ lead to bugs.
92
92
In order to create a new object, one should rather use a factory and construct a
93
93
new object inside of that factory.
94
94
95
- function Foo () {
96
- var obj = {};
97
- obj.value = 'blub ';
95
+ function CarFactory () {
96
+ var car = {};
97
+ car.owner = 'nobody ';
98
98
99
- var private = 2;
100
- obj.someMethod = function(value) {
101
- this.value = value;
99
+ var milesPerGallon = 2;
100
+
101
+ car.setOwner = function(newOwner) {
102
+ this.owner = newOwner;
102
103
}
103
104
104
- obj.getPrivate = function() {
105
- return private ;
105
+ car.getMPG = function() {
106
+ return milesPerGallon ;
106
107
}
107
- return obj;
108
+
109
+ return car;
108
110
}
109
111
110
112
While the above is robust against a missing ` new ` keyword and certainly makes
0 commit comments