How do I work around JavaScript's parseInt octal behavior?

后端 未结 10 1644
Happy的楠姐
Happy的楠姐 2020-11-21 07:36

Try executing the following in JavaScript:

parseInt(\'01\'); //equals 1
parseInt(\'02\'); //equals 2
parseInt(\'03\'); //equals 3
parseInt(\'04\'); //equals          


        
10条回答
  •  无人及你
    2020-11-21 07:59

    Would it be very naughty to replace parseInt with a version that assumes decimal if it has no second parameter? (note - not tested)

    parseIntImpl = parseInt
    parseInt = function(str, base){return parseIntImpl(str, base ? base : 10)}
    

提交回复
热议问题