'javascript'에 해당되는 글 2건
Comparing escape(), encodeURI(), and encodeURIComponent()

자세하게 설명해 놓았네요.
Comparing escape(), encodeURI(), and encodeURIComponent()
The purpose of this article is to examine the differences between these three methods and decide on the appropriate times to use each.
escape() method
Descriptions
MSDN JScript Reference [REF]
The escape method returns a string value (in Unicode format) that contains the contents of [the argument]. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as "%20."
Mozilla Developer Core Javascript Guide [REF]
The escape and unescape functions let you encode and decode strings. The escape function returns the hexadecimal encoding of an argument in the ISO Latin character set. The unescape function returns the ASCII string for the specified hexadecimal encoding value.
Example
The easiest way to understand the behavior is to see it:
escape('~!@#$%^&*(){}[]=:/,;?+\'"\\')
-> %7E%21@%23%24%25%5E%26*%28%29%7B%7D%5B%5D%3D%3A/%2C%3B%3F+%27%22%5C
encodeURI() method
Descriptions
MSDN JScript Reference [REF]
The encodeURI method returns an encoded URI. If you pass the result to decodeURI, the original string is returned. The encodeURI method does not encode the following characters: ":", "/", ";", and "?". Use
encodeURIComponentto encode these characters.
Mozilla Developer Core Javascript Guide [REF]
Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.
Example
The easiest way to understand the behavior is to see it:
encodeURI('~!@#$%^&*(){}[]=:/,;?+\'"\\')->~!@#$%25%5E&*()%7B%7D%5B%5D=:/,;?+'%22%5C
encodeURIComponent() method
Descriptions
MSDN JScript Reference [REF]
The encodeURIComponent method returns an encoded URI. If you pass the result to decodeURIComponent, the original string is returned. Because the encodeURIComponent method encodes all characters, be careful if the string represents a path such as /folder1/folder2/default.html. The slash characters will be encoded and will not be valid if sent as a request to a web server. Use the encodeURI method if the string contains more than a single URI component.
Mozilla Developer Core Javascript Guide [REF]
Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.
Example
The easiest way to understand the behavior is to see it:
encodeURIComponent('~!@#$%^&*(){}[]=:/,;?+\'"\\'):
-> ~!%40%23%24%25%5E%26*()%7B%7D%5B%5D%3D%3A%2F%2C%3B%3F%2B'%22%5C
Summary: What does this mean?
When to use which?
The escape() method does not encode the + character which is interpreted as a space on the server side as well as generated by forms with spaces in their fields. Due to this shortcoming and the fact that this function fails to handle non-ASCII characters correctly, you should avoid use of escape() whenever possible. The best alternative is usually encodeURIComponent().
escape() will not encode: @*/+
Use of the encodeURI() method is a bit more specialized than escape() in that it encodes for URIs [REF] as opposed to the querystring, which is part of a URL. Use this method when you need to encode a string to be used for any resource that uses URIs and needs certain characters to remain un-encoded. Note that this method does not encode the ' character, as it is a valid character within URIs.
encodeURI() will not encode: ~!@#$&*()=:/,;?+'
Lastly, the encodeURIComponent() method should be used in most cases when encoding a single component of a URI. This method will encode certain chars that would normally be recognized as special chars for URIs so that many components may be included. Note that this method does not encode the ' character, as it is a valid character within URIs.
encodeURIComponent() will not encode: ~!*()'
References
MSDN JScript Reference - escape(), encodeURI(), encodeURIComponent()
Mozilla Developer Core Javascript Guide - escape(), encodeURI(), encodeURIComponent()
ASCII Table - http://www.asciitable.com/
W3C's URIs, URLs, and URNs: Clarifications and Recommendations 1.0 - http://www.w3.org/TR/uri-clarification/
Credits: yossarian and Jumper on #javascript (EFNet) for editing and consulting; Joseph Tatroult for additional test cases.
Three common mistakes in JavaScript / EcmaScript

Three common mistakes in JavaScript / EcmaScript
- Undefined is not null.
If you've been writing code in a strongly-typed language recently, you're used to checking the nullity of objects before you use them, like this:
if (SomeObject != null) {
Well, in JavaScript, which is a dynamic language, something that has not been assigned to is not null, it's undefined. Undefined is different from null. Why? Don't ask me. Well, anyways, you can use typeof to explicitly check for undefined, or use other more or less clean tricks, but the best way to deal with that is probably to just rely on the type-sloppiness of JavaScript and count on it to evaluate null and undefined as false in a boolean expression, like this:
if (SomeObject) {
It looks uglier, but it's more robust.
It's very important to keep the undefined case in mind. Another case is when you expect a function to return a boolean value. What if the function forgets to return a value in some cases? Well, its return value is then undefined, which is false. So if your own default value should be true, you should really write this:
if (SomeFunction() !== false) {
Which is different from if (SomeFunction()). Cute, eh? By the way, note the strict equality here.
- You can't overload a function.
Developers who are used to languages like Java and C# overload methods all the time. Well, in JavaScript, there are no overloads, and if you try to define one, you won't even get an error. The interpreter will just pick the latest-defined version of the function and call it. The earlier versions will just be ignored.
The way you simulate overloading is twofold. First, if a parameter is omitted, it is undefined. And second, there is a special variable, arguments, which is an array of the function parameters. Based on the type of each parameter, you can do different things. But it's kind of ugly.
- Undeclared variables are global.
Always, always declare your variables using the var keyword. If you don't, your variable is global. So anyone who makes the same mistake as you (or more likely, if you do the same mistake in two different places) will create nice conflicts which give rise to very difficult-to-track bugs. Even loop counters should be properly declared.

Prev

Rss Feed