File tree Expand file tree Collapse file tree
literals-and-constructors Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2525var good_morning = new Waffle ( ) ;
2626console . log ( typeof good_morning ) ; // "object"
2727console . log ( good_morning . tastes ) ; // "yummy"
28+
29+ // preferred
30+ // You can also guarantee that this situation never occurs by guaranteeing new instances;
31+ // Similar to how you can call $() and $.Deferred() in the same way as new $() and new $.Deferred():
32+ function Waffle ( ) {
33+ if ( ! ( this instanceof Waffle ) ) {
34+ return new Waffle ( ) ;
35+ }
36+
37+ this . tastes = "yummy" ;
38+ }
39+
40+ var good_morning = new Waffle ( ) ;
41+ var good_evening = Waffle ( ) ;
42+
43+ console . log ( typeof good_morning ) ; // "object"
44+ console . log ( good_morning . tastes ) ; // "yummy"
45+ console . log ( typeof good_evening ) ; // "object"
46+ console . log ( good_evening . tastes ) ; // "yummy"
2847</ script >
2948</ body >
3049</ html >
You can’t perform that action at this time.
0 commit comments