Skip to content

Instantly share code, notes, and snippets.

@jonurry
Last active January 20, 2025 11:34
Show Gist options
  • Save jonurry/6113de501d42ac397c6d49008ffb1844 to your computer and use it in GitHub Desktop.
Save jonurry/6113de501d42ac397c6d49008ffb1844 to your computer and use it in GitHub Desktop.
6.4 Borrowing a Method (Eloquent JavaScript Solutions)
let map = {one: true, two: true, hasOwnProperty: true};
// Fix this call
//console.log(map.hasOwnProperty("one"));
console.log(hasOwnProperty.call(map, 'one'));
// → true
@jonurry
Copy link
Author

jonurry commented Feb 28, 2018

6.4 Borrowing a Method

Earlier in the chapter, I mentioned that an object’s hasOwnProperty can be used as a more robust alternative to the in operator when you want to ignore the prototype’s properties. But what if your map needs to include the word "hasOwnProperty"? You won’t be able to call that method anymore because the object’s own property hides the method value.

Can you think of a way to call hasOwnProperty on an object that has its own property by that name?

@jonurry
Copy link
Author

jonurry commented Feb 28, 2018

Hints

Remember that methods that exist on plain objects come from Object.prototype.

And that you can call a function with a specific this binding by using its call method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment