Skip to content

Commit

Permalink
this+object prototypes: addressing getify#143 on the topic of boxed p…
Browse files Browse the repository at this point in the history
…rimitives in 'this' binding calls)
  • Loading branch information
getify committed Jun 16, 2014
1 parent 0488e21 commit e7f31ec
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion this & object prototypes/ch2.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ foo.call( obj ); // 2

Invoking `foo` with *explicit binding* by `foo.call(..)` allows us to force its `this` to be `obj`.

If you pass a simple primitive value (of type `string`, `boolean`, or `number`) as the `this` binding, the primitive value is wrapped in its object-form (`new String(..)`, `new Boolean(..)`, or `new Number(..)`, respectively). This is often referred to as "boxing".

**Note:** With respect to `this` binding, `call(..)` and `apply(..)` are identical. They *do* behave differently with their additional parameters, but that's not something we care about presently.

Unfortunately, *explicit binding* alone still doesn't offer any solution to the issue mentioned previously, of a function "losing" its intended `this` binding, or just having it paved over by a framework, etc.
Expand Down Expand Up @@ -598,7 +600,7 @@ The `this`-binding behavior can in some scenarios be surprising, where you inten

### Ignored `this`

If you pass any non-object value (`null`, `undefined`, `false`, `0`, etc) as a `this` binding parameter to `call`, `apply`, or `bind`, those values are effectively ignored, and instead the *default binding* rule applies to the invocation.
If you pass `null` or `undefined` as a `this` binding parameter to `call`, `apply`, or `bind`, those values are effectively ignored, and instead the *default binding* rule applies to the invocation.

```js
function foo() {
Expand Down

0 comments on commit e7f31ec

Please sign in to comment.