문제

I have the following JavaScript code :

 function dox( )
         {    
          d= [1, 2, 3] + [1,2,3]     ;

          d.map(function(value) {return parseInt(value)});
          document.writeln(d );

      }

and I got the following error :

Uncaught TypeError: undefined is not a function.

I spent a lot of time to solve this bug but I could not ,

Could somebody explain me what is the problem !?

Thanks

도움이 되었습니까?

해결책

d= [1, 2, 3] + [1,2,3]  

This does not concatenate arrays, you need:

var someVar = array1.concat(array2);

If you use + on arrays they will get coerced to strings then concatenated so you actually get a string containing "1,2,31,2,3"

Strings don't have a map function and that's why you get the error

You probably need to put var in front of that d variable so it doesn't leak into the global scope.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top