te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - unterminated string literal in the variable - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - unterminated string literal in the variable - Stack Overflow

programmeradmin3浏览0评论
var MM = '\' + obj[0]['MM '] + '/';

I get two errors while using this code...

missing; before statement and unterminated string literal

var MM = '\' + obj[0]['MM '] + '/';

I get two errors while using this code...

missing; before statement and unterminated string literal

Share Improve this question edited Aug 7, 2011 at 14:19 John Cooper asked Aug 7, 2011 at 7:10 John CooperJohn Cooper 7,64133 gold badges83 silver badges102 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 7

The character \ is "special" because it's used to allow the use of all printable characters in strings. In your case '\' is not a string posed by the only character \, but the beginning of a string starting with the single quote character '.

For exampe if you want the string Hello Andrea "6502" Griffini you can use single quotes

string1 = 'Hello Andrea "6502" Griffini';

and if you want single quotes in the string you can do the opposite

string2 = "Hello Andrea '6502' Griffini";

But what if you want both kind of quotes in the same string? This is where the escape \ character es handy:

string3 = "'llo Andrea \"6502\" Griffini";

Basically \ before a quote or double quote in a string tells javascript that the following character is just a regular character, with no special meaning attached to it.

Note that the very same character is also used in regular expressions... for example if you want to look for an open bracket [ you must prefix it with a backslash because [ in a regular expression has a special meaning.

The escape is also used to do the opposite... in a string if you put a backslash in front of a normal character you are telling javascript that that character is indeed special... for example

alert("This is\na test");

In the above line the \n sequence means a newline code, so the message displayed will be on two lines ("This is" and "a test").

You may now wonder... what if I need a backslash character in my string? Just double it in that case. In your code for example just use '\\'.

Here is a table for the possible meanings of backslash in strings

\"       just a regular double-quote character, it doesn't end the string
\'       just a regular single-quote character, it doesn't end the string
\b       a backspace character (ASCII code 0x08)
\t       a tab character (ASCII code 0x09)
\n       a newline character (ASCII code 0x0A)
\v       a vertical tab character (ASCII code 0x0B)
\f       a form feed character (ASCII code 0x0C)
\r       a carriage return character (ASCII code 0x0D)
\033     the character with ASCII code 033 octal = 27 ("ESC" in this case)
\x41     the character with ASCII code 0x41 = 65 ("A" in this case)
\u05D0   the unicode character 0x05D0 (Aleph from the Hebrew charset)
\\       just regular backslash character, not an escape prefix

\ is an escape character. You'll have to double it to literally mean a backslash character, otherwise it'll augment the following character (In this case the next single quote)

You need to properly escape the backslash:

var lastMenstrualPeriod = '\\' + obj[0]['LastMenstrualPeriod'] + '/';

Being escape character, the JS "piler" is expecting another character to follow, for example \n is newline constant, \t is tab etc.. so \\ is one single backslash in a string.

It is also mentioned in Douglas Crockford book.

You are forgetting to escape '\'

Do this:

var lastMenstrualPeriod = '\\' + obj[0]['LastMenstrualPeriod'] + '/';
发布评论

评论列表(0)

  1. 暂无评论