Octal literals are not available when targeting ECMAScript 5 and higher

别来无恙 提交于 2019-12-22 10:47:09

问题


I am building a norwegaian SSN validator in angularjs and getting the error as 'Octal literals are not available when targeting ECMAScript 5 and higher.' but everything works fine in es3 mode ,please help me with the issue

module ec.directives {
    export function norwegianSsnValidator(){
      return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, element, attrs, ctrl){
            ctrl.$validators.invalidSSN = function(ssn:string){
                if(typeof ssn !== "string"){
                    return false;
                }
                var pno = ssn, day, month, year,
                    ind, c1, c2, yearno;
                // Check length
                if( pno.length != 11 )
                    return false;

                // Split
                day = pno.substr(0,2);
                month = pno.substr(2,2);
                year = pno.substr(4,2);
                ind = pno.substr(6,3);
                c1 = pno.substr(9,1);
                c2 = pno.substr(10,1);
                yearno = parseInt(year);

                if( ind > 0 && ind < 500 ) {
                    yearno += 1900;
                } else if( ind > 499 && ind < 750 && year > 55 && year < 100) {
                    yearno += 1800;
                } else if( ind > 499 && ind < 999 && year >= 00 && year < 40) {
                    yearno += 2000;
                } else if( ind > 899 && ind < 999 && year > 39 && year < 100) {
                    yearno += 1900;
                } else {
                    return false;
                }
                var d1, d2,m1,m2,a1,a2,i1,i2,i3, c1calc, c2calc;
                d1 = parseInt(day.substr(0,1));
                d2 = parseInt(day.substr(1,1));
                m1 = parseInt(month.substr(0,1));
                m2 = parseInt(month.substr(1,1));
                a1 = parseInt(year.substr(0,1));
                a2 = parseInt(year.substr(1,1));
                i1 = parseInt(ind.substr(0,1));
                i2 = parseInt(ind.substr(1,1));
                i3 = parseInt(ind.substr(2,1));

                // Calculate control check c1
                c1calc = 11 - (((3*d1) + (7*d2) + (6*m1) + m2 + (8*a1) + (9*a2) + (4*i1) + (5*i2) + (2*i3)) % 11);
                if( c1calc == 11 )
                    c1calc = 0;
                if( c1calc == 10 )
                    return false;
                if( c1 != c1calc )
                    return false;

                // Calculate control check c2
                c2calc = 11 - (((5*d1) + (4*d2) + (3*m1) + (2*m2) + (7*a1) + (6*a2) + (5*i1) + (4*i2) + (3*i3) + (2*c1calc)) % 11);
                if( c2calc == 11 )
                    c2calc = 0;
                if( c2calc == 10 )
                    return false;
                if( c2 != c2calc )
                    return false;

                return true;
            };
        }
    }
  }
}

回答1:


This is because use of octal literals was supported in ES3 and denoted by a starting 0. Since ES5 these have been deprecated as these are ambiguous and can lead to errors. E.g.:

var o = 0123;
console.log(o); // 83 decimal

Fix: change year >= 00 to year >= 0 as pointed out by nnnnnn

Note: ES6 is going to be better support for octal literals in the form of 0o123 : https://github.com/lukehoban/es6features#binary-and-octal-literals



来源:https://stackoverflow.com/questions/29467148/octal-literals-are-not-available-when-targeting-ecmascript-5-and-higher

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