0
imgFileName = new Array();
imgFileName = imgFileName.push(escape(theFile.name));

assume the theFile.name change every time a function executed.

I console log imgFilename and it return 1?

what I want is storing values into an array by push.

user3346088
  • 109
  • 1
  • 7
  • 1
    you should use imgFileName = [], rather than new Array() http://stackoverflow.com/questions/7375120/why-is-arr-faster-than-arr-new-array – Lee Gary Mar 04 '14 at 03:55
  • @Lee Gary you're talking about minute optimizations. – Ryan Mar 04 '14 at 03:57
  • 1
    it's just some good practices from the beginning IMO, it's generally accepted to create an array like this [] – Lee Gary Mar 04 '14 at 04:00

3 Answers3

2

The array .push() method returns the length of the array after the element is added to the array, so you are changing the imgFileName variable to hold the length of the array.

You could do:

var imgFileNames = [];
imgFileNames.push(escape(theFile.name));

And if you need to work with the file name:

var imgFileNames = [];
var fileName = escape(theFile.name);
imgFileNames.push(fileName);

Just to be clear though, you are not going to want to create the array every time right before you push a file name onto it. You will want to create it once.

John S
  • 21,212
  • 8
  • 46
  • 56
0

You're reassigning the imgFileName when you assign it the operator =;

imgFileName = new Array();
imgFileName.push(escape(theFile.name));

Fiddle

Ryan
  • 14,392
  • 8
  • 62
  • 102
0

you just push the value no need to assign it , push method returns the new length of the array

if you try to do like imgFileName=imgFileName.push(escape("name"));

imgFileName will always returns the length

imgFileName = new Array();
imgFileName.push(escape(theFile.name));
console.log(imgFileName);
Sarath
  • 608
  • 4
  • 12