Several issues that need special attention when using prototype. js.

Source: Internet
Author: User

1. String. prototype. camelize BUG
This method is used to return the camel writing of a string. It is often used to control the style of Elements Using js.
For example
Var ss = "font-color"
Ss = ss. camelize () // fontColor
In general, camelize works well, but there is a special column, that is, floating position float.
Var ss = "float"
Obj. style [ss. camelize ()] = "right" // This causes an error.
Apparently, the author did not consider the special case of float. The correct method is:
Ie: obj. style. styleFloat = "right"
Ff: obj.style.css Float = "right"
2. String. prototype. inspect BUG
Here, the inspect method has a Bug. The author does not use regular expressions when using the replace method. As a result, only the first matching character can be replaced.
The correct statement should be as follows:
Inspect: function (){
Return "'" + this. replace (// \/g ,'\\\\'). replace (/"/g, '\\\"') + "'";
}
3. Array. prototype. all BUG
This method checks whether all elements in the array can make the iteration function true. True is returned if all conditions are met. Otherwise, false is returned.
Var f = function (x) {return x % 2 = 0} // check whether a number is an even number.
Var arr = [2, 4, 6]
Alert (arr. all (f) = true) // display true
However, if arr is empty, true is returned.
Var arr = []
Alert (arr. all (f) = true) // display true
4. Array. prototype. any BUG
Same as the all method, true is still returned for an empty array.
5. Array. prototype. detect is the find method.
This is not a Bug, but it is easy to misunderstand without looking at the original code.
This method finds the first element that can satisfy the iteration function and returns the value of the element.
Misunderstanding 1:
Var f = function (x) {return x % 2 = 0} // checks whether the result is an even number.
Var arr = [,] // deliberately defines an array with an odd number.
Alert (arr. find (f) = false) // false
// Many people mistakenly think that the find function will return false if it cannot find an element that meets the conditions. In fact, it returns "undefined"
Misunderstanding 2:
Search for numeric elements in the array
Var f = function (x) {return! IsNaN (x) & typeof (x) = "number "}
Var arr = [false, "go_rush", 0, "A shun"]
If (arr. find (f) alert ("the array contains number elements") // In fact, this alert will never be executed.
Because the find method returns the first qualified value. Return Value: 0. So .....

6. The processing of hash objects requires extra caution.
Var hash = {member: 1, test: 2, ids: 3}
Alert (hash. member)
Alert ($ H (hash). inspect ())
Alert ($ H (hash). toQueryString ())
// Hash. member actually exists, but. inspect () and. toQueryString () do not exist.
The following attributes will also conflict with prototype. js.
Each, all, any, collect, detect, findAll, grep, include, inject, invoke, max, min,
Partition, pluck, reject, sortBy, toArray, zip, inspect, map, find, select, member, entries

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.