Last active
January 20, 2025 11:34
-
-
Save jonurry/6113de501d42ac397c6d49008ffb1844 to your computer and use it in GitHub Desktop.
6.4 Borrowing a Method (Eloquent JavaScript Solutions)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let map = {one: true, two: true, hasOwnProperty: true}; | |
// Fix this call | |
//console.log(map.hasOwnProperty("one")); | |
console.log(hasOwnProperty.call(map, 'one')); | |
// → true |
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
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 thein
operator when you want to ignore the prototype’s properties. But what if yourmap
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?