开发者

What does '\' mean in a JavaScript function parameter?

I have been asked to document some code. Some javascript functions pass parameters l开发者_运维技巧ike

onclick='showhide(<%#String.Format("\"#customer{0}\"",Container.DataItemIndex) %>);'
  1. What is the purpose of "\" in the code?
  2. Are they some special kind of escape characters? Would the code fail if we remove them?


They are 'escaping' the quotation marks, so they can be included in the string. Otherwise they would be confused with the start/end quotation marks of the string.

http://en.wikipedia.org/wiki/Escape_character


This doesn't look like (pure) JavaScript, but instead like some other language that produces JavaScript code (probably ASP.NET):

Assuming that <%# %> is that languages code to insert the result of the contained statement into the text, this means that the result of

String.Format("\"#customer{0}\"",Container.DataItemIndex)

will be written between the closing and the opening parenthesis of the function call.

This means that the escape character \ isn't use in JavaScript here, but in the host language (probably C# or VB.NET). The meaning is probably the same as in JavaScript, 'though: it escapes the double-quote to allow it to be represented inside a string literal.


  1. What is the purpose of "\" in the code ?

Seems like escape sequences to me. Assuming that this is C# code, the string in your code:

"\"#customer{0}\""

is interpreted by the compiler as:

"#customer{0}"

(this INCLUDES the double quotes)

When this is response.written, the output will become:

onclick='showhide("#customer_1234");'


Yes, it is an escape character.

In this case it is escaping the " character.


It is an escape sequence used in the above code.

When written in string \" is equivalent to ". i.e.,

<%#String.Format("\"#customer{0}\"",Container.DataItemIndex) %>

would render something like this if DataItemIndex is 9

"#customer9"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜