简体   繁体   中英

How can I string split to return array in chrome?

I got strange behaviour in chrome, (ff & ie works :) )

Observe:

 function doSomething(){
   var status = "completed,please fix,qualified,cancel".split(',');
   $('.searchControls .status').html(status.join(','));
 }

Uncaught TypeError: Object completed, please fix,qualified,cancel has no method 'join'

How can I fix this? Am I doing something wrong?

Seems that it's a case of name clashing. There is a window.status property that transforms everything to string. Observe:

> window.status
 => ""
> var status = 1
 => undefined
> window.status
 => "1"
> var status = ['completed', 'cancel']
 => undefined
> window.status
 => "completed,cancel"

Solution: choose another name. my_status , for example.

I believe window.status is a reserved word and therefore causes errors. Either wrap the scope of the "status" variable to something else than "window" or try renaming the variable, eg.

var myStatus = "completed,please fix,qualified,cancel".split(',');
 $('.searchControls .status').html(myStatus.join(','),  1);

...and it works.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM