質問

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