Value assigned to primitive will be lost

前端 未结 4 1475
感动是毒
感动是毒 2020-12-17 08:09

If I have an array of objects, and loop through them assigning a value to an attribute for each one, WebStorm warns me:

Values assigned to primitive w

4条回答
  •  悲&欢浪女
    2020-12-17 08:38

    There's nothing improper in your code, WebStorm's type inference is getting a bit confused (this aspect of JavaScript is particularly confusing).

    Its linter sees a string and assumes you will try something like this:

    var primitive = "september";
    primitive.vowels = 3;
    
    primitive.vowels;
    // => undefined
    

    Which would lead to a 'lost' value.

    The fact that it only catches this 'error' inside of a function seems like an outright bug that should be reported.

    To further understand this weird part of JavaScript, I recommend Angus Croll's excellent in-depth article here.

提交回复
热议问题