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

Unicode naming for methods seems unsupported. Some warning will be appreciated. #6975

Closed
nobodxbodon opened this issue Oct 31, 2017 · 10 comments

Comments

@nobodxbodon
Copy link

Version

2.5.2

Reproduction link

https://jsfiddle.net/p48cLbe3/2/

Steps to reproduce

    <script src="https://unpkg.com/vue"></script>
    <div id="元素id5">
      <p>{{ 问好 }}</p>
      <button v-on:click="改口">改口</button>
    </div>
var 应用5 = new Vue({
  el: '#元素id5',
  data: {
    问好: '你好'
  },
  methods: {
    改口: function () {
      this.问好 = '吃了么'
    }
  }
})

What is expected?

Excuse me if this is closer to feature request.

It'll be great if method can support unicode naming. Otherwise it may be better to provide some feedback.

Thanks for your efforts.

What is actually happening?

I'm guessing validation passes because the method does exist, but somehow invoking method fails.

@javoski
Copy link
Member

javoski commented Oct 31, 2017

You can try

<button @click="改口()"></button>

if you really want this.

@nobodxbodon
Copy link
Author

@javoski thanks. As I'm new to Vue, may I ask what's the difference between these two ways?

@posva
Copy link
Member

posva commented Oct 31, 2017

It's just a workaround
by default Vue will call the method passing $event to it if you pass the function instead of calling it: @click="doIt" -> @click="doIt($event)"

@HerringtonDarkholme
Copy link
Member

HerringtonDarkholme commented Nov 2, 2017

I don't think we can afford warning in runtime build, though :/

https://stackoverflow.com/a/9392578/2198656

if you were to summarize all these (variable naming) rules in a single ASCII-only regular expression for JavaScript, it would be 11,236 characters long (for ECMAScript 5.1 / Unicode 6.1.0)

@nobodxbodon
Copy link
Author

As the alternative method suggested above does work, it just occurred to me that v-on:click="改口()" might work, and as I just tried it does work! While v-on:click="改口" doesn't.

@9percent
Copy link

9percent commented Nov 2, 2017

By tracking the click event, it looks like Vue does treat the Unicode names differently behind the scene, while the method name is like "doIt", button click invokes the "boundFn" directly, but when the method name is an Unicode name, the button invokes something like a "click event handler" instead:

on: {
    "click": function($event) {
        改口
    }
}

nobodxbodon pushed a commit to program-in-chinese/vuejs_guide_zh that referenced this issue Nov 2, 2017
@yyx990803
Copy link
Member

yyx990803 commented Nov 20, 2017

In order to use the method name directly (shorthand) it needs to be a alphanumeric identifier. Any expression that is not an alphanumeric identifier is treated as a function body instead, which is why in this case () is needed. Adding shorthand support for full unicode, as @HerringtonDarkholme has pointed out, is too expensive and not worth it.

@nobodxbodon
Copy link
Author

Thanks for addressing this. Still, I wonder if some feedback could be provided in this case, maybe something like "You used a non-alphanumeric identifier. If it's intended, add () as a workaround."?

@yyx990803
Copy link
Member

The problem it's too expensive to detect "a non-alphanumeric identifier (maybe a unicode identifier)" from "a full expression"

@nobodxbodon
Copy link
Author

Excuse me if I miss something obvious. I realize it's too expensive to decide if an identifier is legit unicode identifier, as pointed out above. However I thought detecting if an identifier is non-alphanumeric is easier, like checking if it matches /[A-Za-z_$][\w$]*/. That would require developer to check if it's legit unicode identifier, but having some feedback would be better than none, I suppose?
Anyways, as unicode naming is not at all in wide use, plus it does have alternative solution(with ()), thanks to the community support, I'd totally understand if this edge case has too low priority to be picked up in any time soon.
Thanks again for your patience and time!

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

No branches or pull requests

6 participants