Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array.prototype.lengthをアクセッサプロパティで再現するのサンプルコードをリファクタリングした #1626

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

haruguchi-yuma
Copy link
Contributor

クラスのArray-likeオブジェクトの項(https://jsprimer.net/basic/class/#array-like-length)で
this.items.lengthの部分はthis(ArrayLikeクラスのインスタンス)にgetterのlengthが生えているため、可読性を考慮してthis.lengthに変更しました。

ご検討ください。

this.items.lengthの部分はthisにgetterのlengthが生えているため、可読性を考慮してthis.lengthに変更した
@bot-user
Copy link

Deploy Preview for js-primer ready!

Name Link
🔨 Latest commit ef16324
🔍 Latest deploy log https://app.netlify.com/sites/js-primer/deploys/63fcd65857a0be0008cf28ca
😎 Deploy Preview https://deploy-preview-1626--js-primer.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

const currentItemLength = this.items.length;
const currentItemLength = this.length;
Copy link
Collaborator

@azu azu Feb 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人的には下で this.items.slice(0, newLength);this.items を参照してるので、ここでは this.itemslength を見るのが正しいかなーとは思いました
(get length()と get items()の実装が異なる可能性があるのと、一貫性)
また、currentItemLength なので this.length だとItemのlengthだとちょっとわかりにくいというのもあります。

あと、setterの中で同じ名前のgetterを参照するのはなんか無限ループ感があってちょっと怖いというが感覚的に避けたい理由ですかね。

Copy link
Collaborator

@azu azu Feb 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多分一番良いのは、getter/setter内からはgetter/setterを触らないようにするという感じにして、ここでもthis._itemsを常に使うとかなのかなーとはちょっと思いました。

getter/setterそのものが可読性的には微妙なので(見た目で区別ができない)、改善するとしたら一貫性なのかなーとは思いました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants