I've noticed something weird. I've always thought new Array()
was the same as {}
, however it seems to be different as {}
seems to just be an object type whereas new Array()
is an Array in the Chrome debugger.
So I've been using $.param(data)
, where data is the data from a $.ajax()
call. I notice that when i have a params1 = new Array()
and a params2 = {}
inside the data, they come out differently.
params1 becomes
params1[]=1¶ms1[]=2
and params2 becomes
params2[0]=1¶ms2[1]=2.
The problem is that I had been using .param(data, false)
because I noticed that params1[]
was being serialized incorrectly, however .param(data, false)
fails for params2
and gives me params2=[object+Object]
.
I figure I can get around this by just using .param(data) and stripping out "[]" so that regardless of it being initialized using {}
or new Array
, it'll still work out correctly. But I'd like to know if there's a better solution (short of always using {}
vs new Array
).
Kyliod,
In javascript {} is shorthand for creating a new Object, and [] is shorthand for a "new Array()."
SO:
var myArray1 = [];
var myArray2 = new Array();
var myObject = {};
myObject.objVariable1 = 'some string or other variable data';
var myObject2 = { obj2Var1 : 'some string', obj2Var2 : 1234, obj2Var3 : true };
// do stuff
var thing1 = myArray1[1]; // get something out of myArray1
var thing2 = myArray2[2]; // get something out of myArray2
var thing3 = myObject.objVariable1; // get something out of myObject
if(myObject2.obj2Var3)
{
// do other stuff
}
Hopefully this helps you clear up your jQuery / javascript Ajax issues.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.