@@ -11,74 +11,74 @@ constructor.
1111If the function that was called has no explicit ` return ` statement, then it
1212implicitly returns the value of ` this ` - the new object.
1313
14- function Foo( ) {
15- this.bla = 1 ;
14+ function Person(name ) {
15+ this.name = name ;
1616 }
1717
18- Foo .prototype.test = function() {
19- console.log(this.bla );
18+ Person .prototype.logName = function() {
19+ console.log(this.name );
2020 };
2121
22- var test = new Foo ();
22+ var sean = new Person ();
2323
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` .
2626
2727In case of an explicit ` return ` statement, the function returns the value
2828specified by that statement, but ** only** if the return value is an ` Object ` .
2929
30- function Bar () {
31- return 2 ;
30+ function Car () {
31+ return 'ford' ;
3232 }
33- new Bar (); // a new object
33+ new Car (); // a new object, not 'ford'
3434
35- function Test () {
36- this.value = 2;
35+ function Person () {
36+ this.someValue = 2;
3737
3838 return {
39- foo: 1
39+ name: 'Charles'
4040 };
4141 }
42- new Test(); // the returned object
42+ new Test(); // the returned object ({name:'Charles'}), not including someValue
4343
4444When the ` new ` keyword is omitted, the function will ** not** return a new object.
4545
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!
4848 }
49- Foo (); // undefined
49+ var somePirate = Pirate (); // somePirate is undefined
5050
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
5353* global object* as the value of ` this ` .
5454
5555### Factories
5656
5757In order to be able to omit the ` new ` keyword, the constructor function has to
5858explicitly return a value.
5959
60- function Bar () {
61- var value = 1 ;
60+ function Robot () {
61+ var color = 'gray' ;
6262 return {
63- method : function() {
64- return value ;
63+ getColor : function() {
64+ return color ;
6565 }
6666 }
6767 }
68- Bar .prototype = {
69- foo : function() {}
68+ Robot .prototype = {
69+ someFunction : function() {}
7070 };
7171
72- new Bar ();
73- Bar ();
72+ new Robot ();
73+ Robot ();
7474
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
7676has a property called ` method ` , which is a
7777[ Closure] ( #function.closures ) .
7878
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
8080prototype 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.
8282
8383In the above example, there is no functional difference between using and
8484not using the ` new ` keyword.
@@ -92,19 +92,21 @@ lead to bugs.
9292In order to create a new object, one should rather use a factory and construct a
9393new object inside of that factory.
9494
95- function Foo () {
96- var obj = {};
97- obj.value = 'blub ';
95+ function CarFactory () {
96+ var car = {};
97+ car.owner = 'nobody ';
9898
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;
102103 }
103104
104- obj.getPrivate = function() {
105- return private ;
105+ car.getMPG = function() {
106+ return milesPerGallon ;
106107 }
107- return obj;
108+
109+ return car;
108110 }
109111
110112While the above is robust against a missing ` new ` keyword and certainly makes
0 commit comments