{"push_id":839713298,"size":1,"distinct_size":1,"ref":"refs/heads/Chp_18EnsuringCorrectUseOfCallApplyBinding","head":"b99ae6dda3f4a6dbe0aa06126fdab30bffaaa5eb","before":"52ce30c612814ec871cdc41389accc4e2f0005bc","commits":[{"sha":"b99ae6dda3f4a6dbe0aa06126fdab30bffaaa5eb","author":{"email":"688085edcac35a6f5d4cd9ba4db0e893cb0d2736@gmail.com","name":"petrero"},"message":"  You may recall that the checkedInAttendeeCounter module (also created in Chapter 5) had an issue related to its use of this within the callback function provided to iterate , as you can see in the following example:\nvar checkInService =\nConference.checkInService(Conference.checkInRecorder()),\n  attendees = Conference.attendeeCollection(),\n  counter = Conference.checkedInAttendeeCounter();\n  // Add attendees selected in the UI to the attendee collection\n  attendees.add(Conference.attendee('Pete', 'Mitchell'));\n  attendees.add(Conference.attendee('Nick', 'Bradshaw'));\n  // check the attendees in\n  attendees.iterate(checkInService.checkIn);\n\n  // count the checked-in attendees\n  attendees.iterate(counter.countIfCheckedIn);\n\n  console.log(counter.getCount()); // 0 (  Please note that because the focus of the polyfill implementation is the illustration of concepts rather than the actual replication of functionality, the polyfill that is created in this section should not be considered anything other than an incomplete sample; do not use it in real code!)\n\n  As mentioned earlier in this chapter, the fact that the countIfCheckedIn method is defined as part of the counter object does not necessarily mean that this will be bound to the counter object within countIfCheckedIn . In fact, because of the way the attendeeCollection ’s iterate function is written, it is guaranteed that this will be bound to the global window object; the countIfCheckedIn method is invoked via the reference callback within iterate .\n  In Chapter 5, the this binding issue was addressed by retaining a reference to the instance of the checkedInAttendeeCounter in a variable named self , and using self in countIfCheckedIn rather than this . (See the section  in Chapter 5 for a refresher.) Doing so ensured that countIfCheckedIn would work correctly even if this ended up not being bound to the counter object due to call site details.\n  Another solution would have been to leverage Array.prototype.forEach ’s ability to accept a context object to be bound to this within the callback. That sounds strikingly familiar to how call and apply function, doesn’t it? One might conclude that Array.prototype.forEach uses call under the covers. Unfortunately for you and Charlotte, Array.prototype.forEach wasn’t added to the language until ECMAScript 5. As such, it’s a feature that Internet Explorer 8 doesn’t support. According to NetMarketShare ( http://marketshare.hitslink.com ) Internet Explorer 8 was responsible for over 17 percent of web traffic in October 2014. With this number in mind, the organizers of the JavaScript conference want to ensure that the conference website supports Internet Explorer 8. Does this mean forEach can’t be used? No, it doesn’t.\n  Differences in browser support of ECMAScript features are regularly addressed using polyfills. A polyfill is a piece of code that implements a language feature that a developer expects to be implemented natively by the browser. A polyfill detects whether or not the language feature exists, and if the feature isn’t present the polyfill plugs itself in the place of the feature. If a developer includes polyfills with her project, she is assured that the language feature the polyfill implements will be present even if the browser doesn’t natively implement it.\n  Because Internet Explorer 8 doesn’t support Array.prototype.forEach , and Array.prototype.forEach has already been used in the JavaScript conference website’s code, a polyfill needs to be provided.\n  NOTE There are plenty of polyfills that exist for ECMAScript 5 features,including Array.prototype.forEach . While we don’t advocate reinventing the wheel, implementing a polyfill for Array.prototype.forEach provides an excellent vehicle for creating and testing code that uses Function.prototype.call .\n  Please note that because the focus of the polyfill implementation is the illustration of concepts rather than the actual replication of functionality, the polyfill that is created in this section should not be considered anything other than an incomplete sample; do not use it in real code!\n  The first step in creating the polyfill for Array.prototype.forEach (even before writing unit tests) is to determine the interface and behavior of the feature as it’s implemented in ECMAScript 5. The annotated ECMAScript 5 specification of Array.prototype.forEach is available at http://es5.github.io/#x15.4.4.18 . Some of the key takeaways from that page are:\n * The method signature is Array.prototype.forEach(callbackFcn [, thisObj]) .\n * The thisObj parameter is optional. If it is provided, it will be provided as the this object for each invocation of callbackFcn . If it is not provided, undefined will be provided to callbackFcn as the this object.\n * The callback function callbackFcn will be executed once per element in the array, with the three arguments:\n * The value of the element\n * The index of the element\n * The object being traversed","distinct":true,"url":"https://api.github.com/repos/pyterDiaconescu/ReliableJavascript/commits/b99ae6dda3f4a6dbe0aa06126fdab30bffaaa5eb"}]}