Efficiently find duplicate data property in object literal?

倖福魔咒の 提交于 2019-12-24 23:57:43

问题


After creating our new system in Vue with babel, I have started testing compatibility towards older devices. Our babel is transpiling the source down to es2015 together with webpack.

I have now tested with browserstack against both Ios & android. Newer OS works on both platforms. However on android phones that use the default browser, I get an error in sentry stating; Duplicate data property in object literal not allowed in strict mode It does not give me any hints on where this might be thus making the debugging process painfully hard.

The only light in the end of the tunnel I can se now is the ios part. Ios devices that run IOS < 9 states an error Attempted to redefine property 'value'. also in sentry

If I am not mistaking, the ios issue is a reworded error of the same issue? As I read over here, I suppose 'value' might be defined twice in a object or element.

This all wraps up to the question, how does one go with finding duplicate data properties?


回答1:


Can you share some of your code (just the area from a few components?)

One thing to check is inside of data(), ensure you are returning an object. This happened to me when I started out with Vue.

Example:

// component a
data () {
  a: ''
}

// component b 
data () {
  a: '' // ERROR! Duplicate
}

This happens because the data is merged on the main Vue instance. So in this case it should look like:

// component a
data () {
  return {
    a: ''
  }
}

// component b 
data () {
  return {
    a: '' // ok now
  }
}

Hard to make any other guesses without some code.




回答2:


I managed to find out what line the error occurred on and found out that a plugin that I used with name Vue-numeric had created a duplicate value:

 domProps: {
   value: n.value,
   value: n.amount
 },

I had accidentally locked the plugin on a older version where this problem was present. The issue was fixed by simply updating.

Thank you @xenetics for taking your time on this issue.




回答3:


Yes, this is a restriction that was only in effect in ES5 strict mode, which these environments you have apparently use. It absolutely makes sense but was nonetheless loosened in ES6 because of computed properties - see What's the purpose of allowing duplicate property names? for details. That's why babel doesn't complain about it when transpiling.

To find these (valid but nonsensical) duplicate property names in object literals in your code base, you can use a linter such as ESLint with a rule against these.



来源:https://stackoverflow.com/questions/47197535/efficiently-find-duplicate-data-property-in-object-literal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!