Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: change the condition within the Animal[Symbol.hasInstance] to match the purpose of the method#3682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
katlinfall wants to merge1 commit intojavascript-tutorial:master
base:master
Choose a base branch
Loading
fromkatlinfall:bugfix/symbol-has-instance-example

Conversation

katlinfall
Copy link

Description

According to the comment above theclass Animal, the purpose of theAnimal[Symbol.hasInstance] method is to "assume that anything with the canEat property is an animal". The conditionif (obj.canEat) return true; doesn't fit the purpose mentioned, because it checks whether!!obj.canEat === true. So if we create the object with falsycanEat property value (for example:let obj = { canEat: false};), thenobj instanceof Animal will returnfalse, meaning thatobj is not the instance ofAnimal and the condition "anything with the canEat property is an animal" is not true.
I suggest replacing theif (obj.canEat) return true; condition withif ('canEat' in obj) return true;. Thein operator returnstrue if the specified property is in the specified object or its prototype chain. With the new condition,obj instanceof Animal will returntrue even if the value of thecanEat property isfalse, which is exactly the same as saying "anything with the canEat property is an animal".
P.S. There is also ahasOwnProperty() method, which returns a boolean indicating whether this object has the specified property as its own property (as opposed to inheriting it). This method is not suitable for the implementationAnimal[Symbol.hasInstance], because the conditionif (obj.hasOwnProperty('canEat')) will be truthy only if the propertycanEat is a property of theobj itself, but not of its prototypes.

Links

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty

anujsngh reacted with thumbs up emoji
@javascript-translate-botjavascript-translate-bot added the review neededReview needed, please approve or request changes labelApr 10, 2024
@javascript-translate-botjavascript-translate-bot requested a review froma teamApril 10, 2024 22:25
@CLAassistant
Copy link

CLAassistant commentedApr 10, 2024
edited
Loading

CLA assistant check
All committers have signed the CLA.

@smith558smith558 self-assigned thisMay 5, 2024
@smith558smith558 added the P1High priority labelMay 5, 2024
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees

@smith558smith558

Labels
P1High priorityreview neededReview needed, please approve or request changes
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@katlinfall@CLAassistant@smith558@javascript-translate-bot

[8]ページ先頭

©2009-2025 Movatter.jp