Skip to content

Make the statement less definitive #1190

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

Merged
merged 2 commits into from
Oct 7, 2017
Merged

Make the statement less definitive #1190

merged 2 commits into from
Oct 7, 2017

Conversation

phanan
Copy link
Member

@phanan phanan commented Oct 6, 2017

The current statement reads:

Since arrow functions are bound to the parent context, this will not be the Vue instance as you'd expect and this.a or this.myMethod will be undefined.

which is true in most, but not all, of the cases. If the parent context by any chance has a or myMethod, this.a and this.myMethod won't be undefined: https://jsfiddle.net/phanan/Lwh1sye2/1/

This PR aims to make the statement less definitive.

@chrisvfritz
Copy link
Contributor

Hmm, I worry that this change doesn't actually help anyone and simply increases the complexity of the sentence. This warning is really only for people who don't understand context in JavaScript and those people lack the background information to answer the questions this qualifier raises:

  • "Does this mean sometimes the property will be defined with what we expect?"
  • "In what situations will it be defined?"
  • "Does this mean there's a technique I could use to make everything defined on the parent context?"

Our target audience can't answer these questions for themselves and we're not interested in answering them in the docs, so it ends up just being a distraction from the central point: that they shouldn't use arrow functions in these cases, because properties on this won't work.

Does that make sense?

@phanan
Copy link
Member Author

phanan commented Oct 7, 2017

While I understand the point of not fixing something not broken, the technical knowledge of the tip is IMO, strictly saying, incorrect. Let's review what we have here:

Since arrow functions are bound to the parent context, this will not be the Vue instance as you'd expect and this.a or this.myMethod will be undefined.

We're definitively saying this.a and this.myMethod will be undefined when it's not necessarily the case. Technically speaking, this makes the statement wrong.

How about tweaking the expression a bit to maintain both the correctness and simplicity? Something like:

Since arrow functions are bound to the parent context, this will not be the Vue instance as you'd expect and this.a or this.myMethod will refer to the parent's property or method, which is unlikely what you'd want.

Thoughts?

@chrisvfritz
Copy link
Contributor

chrisvfritz commented Oct 7, 2017

I agree that your revision is more technically correct. 🙂 I just worry that it raises more questions than answers for anyone the paragraph is relevant to. I like where you're going with the compromise though!

One missing detail I think is important is the note about "undefined", because the property will be undefined 99.9% of the time and I've seen students do a Google or in-page search that includes undefined when they see a TypeError in these cases. Removing it would make these notes less searchable.

So now that you have me thinking, what would you think about the following?

Since arrow functions are bound to the parent context, this will not be the Vue instance and this.a or this.myMethod will not be defined as you'd expect, often resulting in errors such as:

Uncaught TypeError: Cannot read property of undefined
Uncaught TypeError: this.myMethod is not a function

I think that might improve the clarity, accuracy, and searchability.

@phanan
Copy link
Member Author

phanan commented Oct 7, 2017

Sounds good to me. If we could somehow eliminate one instance of "as you'd expect" it would be perfect!

@chrisvfritz
Copy link
Contributor

chrisvfritz commented Oct 7, 2017

Good catch! I just updated my comment. 🙂

@phanan
Copy link
Member Author

phanan commented Oct 7, 2017

Reworded as such. I've made the errors inline, as our p.tip doesn't support code blocks.

@chrisvfritz chrisvfritz merged commit ef7ba7d into vuejs:master Oct 7, 2017
phanan added a commit to vuejs-vn/vuejs.org that referenced this pull request Oct 10, 2017
* Replace `true` with truthy (vuejs#1196)

* Change "Registration" to "Global Registration" (vuejs#1192)

* typos (vuejs#1191)

* [Website]: Let menu to be above add (vuejs#1188)

Signed-off-by: Bruno Lesieur <[email protected]>

* Fix: Demystified markdown logic (vuejs#1186)

* Fix: Demystified markdown logic

I have changes jsfiddle link with a similar jsfiddle. 

The difference between two jsfiddle is in javascript code, `marked()` has a comment mentioning its source as external library. 

This is important because it is easy to assume that `marked()` cmes from an external library instead of Vue, especially for a beginner.

* make dependencies apparent in markdown example

* Correct the heading tags (vuejs#1181)

correct the opening and closing `h4` tags

* Make the statement less definitive (vuejs#1190)

* Make the statement less definitive

* Reword the tip

* add SG exception where kebab-case everywhere is also acceptable for components in templates

* revise single-file components, with new example sandbox

* Add Additional Resources to ecosystem dropdown (vuejs#1182)

* Add additional resources to dropdown

* reorganize ecosystem dropdown, adding Tooling

* Move roadmap to News section of ecosystem dropdown

* Update "Core Plugins" to "Core Libraries"

* rename SG complex expressions rule to simple

* rename SG complex computed expressions rule to simple

* address SG question about coupled component names, fixes vuejs#1195

* fix SG typos, fixes vuejs#1194

* make it more obvious in SG that details can be expanded, fixes vuejs#1170

* Update custom-directive.md (vuejs#1198)

I suggest add this params demo because i can not understand when i saw this part,i think other people will have the same confusion

* clarified titles of SG priority D rules

* Re-translate the arrow function tip
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.

2 participants