What does = +_ mean in JavaScript

前端 未结 12 1729
南笙
南笙 2020-11-22 11:37

I was wondering what the = +_ operator means in JavaScript. It looks like it does assignments.

Example:

hexbin.radius = function(_)         


        
12条回答
  •  感情败类
    2020-11-22 12:20

    It's a sneaky one.

    The important thing to understand is that the underscore character here is actually a variable name, not an operator.

    The plus sign in front of that is getting the positive numerical value of underscore -- ie effectively casting the underscore variable to be an int. You could achieve the same effect with parseInt(), but the plus sign casting is likely used here because it's more concise.

    And that just leaves the equals sign as just a standard variable assignment.

    It's probably not deliberately written to confuse, as an experienced Javascript programmer will generally recognise underscore as a variable. But if you don't know that it is definitely very confusing. I certainly wouldn't write it like that; I'm not a fan of short meaningless variable names at the best of times -- If you want short variable names in JS code to save space, use a minifier; don't write it with short variables to start with.

提交回复
热议问题