Is the call object is an array, the array type added a prototype of the function, generally write some extensions often used. such as determining whether an element is in an array or something.
Array.prototype.inArray=
function
(value){
for
(
var
i=0;i<
this
.length;i++){
if
(
this
[i] == value){
return
true
;
}
}
return
false
};
var
arr=[
"1"
,
"2"
,
"3"
];
//以下同调用方式,在inArray函数中,使用this即可得到arr
arr.inArray(
"1"
);
Summary of three common methods of JS array de-weight
The first is a more conventional approach.
Ideas:
1. Build a new array to store the results
Each time an element is removed from the original array in a 2.for loop, the loop is compared with the result array.
3. If the element is not in the result array, it is stored in the result array
Copy CodeThe code is as Follows: [this[0]]; if it is an array, it refers to the first element of This.
Array.prototype.unique1 = function () {
var res = [this[0]];
for (var i = 1; i < this.length; i++) {
var repeat = false;
for (var j = 0; J < res.length; J + +) {
if (this[i] = = res[j]) {
repeat = true;
Break
}
}
If (!repeat) {
Res.push (this[i]);
}
}
Return res;
}
var arr = [1, ' A ', ' a ', ' b ', ' d ', ' e ', ' e ', 1, 0]
Alert (arr.unique1 ());
Console
var arr = [1, ' A ', ' a ', ' b ', ' d ', ' e ', ' e ', 1, 0]
Undefined
var res = [arr[0]];
Undefined
Res
[1]
var pattern =/aaaa/,
str = ' AAAA ';
Console.log (pattern.test (str));
Vm1599:3 true
Undefined
The second method is more efficient than the above method
Ideas:
1. Sort the original array first
2. Check that the first element in the original array is the same as the last element in the result array, because it is already sorted, so the repeating element is in the adjacent position
3. If it is not the same, the element is stored in the result array
Copy CodeThe code is as Follows:
Array.prototype.unique2 = function () {
This.sort (); Sort first
var res = [this[0]];
for (var i = 1; i < this.length; i++) {
If (this[i]!== res[res.length-1]) {
Res.push (this[i]);
}
}
Return res;
}
var arr = [1, ' A ', ' a ', ' b ', ' d ', ' e ', ' e ', 1, 0]
Alert (arr.unique2 ());
The second method also has some limitations because it is sorted before going to heavy, so the final return of the de-weight result is also Sorted. This method is not available if the order of the array is not changed to be heavy.
Third Method (recommended)
Ideas:
1. Create a new array to hold the result
2. Create an empty object
3.for loop, Each time an element is taken out and compared to the object, if the element is not duplicated, it is stored in the result array, and the content of the element as an object property, and assigned a value of 1, deposited in the 2nd step of the object Created.
Note: as for how to compare, it is to remove an element from the original array, and then go to the object to access the property, if you can access the value, then the description is Repeated.
Copy CodeThe code is as Follows:
Array.prototype.unique3 = function () {
var res = [];
var json = {};
for (var i = 0; i < this.length; i++) {
If (!json[this[i]]) {
Res.push (this[i]);
json[this[i]] = 1;
}
}
Return res;
}
var arr = [112,112, 34, ' Hello ', 112,112, 34, ' Hello ', ' str ', ' str1 '];
Alert (arr.unique3 ());
JS using regular expressions to remove repeating characters in a string
<script language=
"javascript"
>
str =
"Google"
str1 = str.replace(/(.).*\1/g,
"$1"
)
document.write(str +
"<br>"
);
document.write(str1);
</script>
- prototype (js Object)
In javascript, the prototype object is an important mechanism for implementing object-oriented. Each function is an object (function), which has a sub-object prototype object, and the class is defined in the form of a Function. Prototype represents the prototype of the function, and also represents a collection of members of a class. When you create an instance object of a class from new, the members of the prototype object become members of the instantiated Object. 1, the object is referenced by the class, only the function object can be referenced, 2, after the new instantiation, its members are instantiated, the instance object can be Called. At the same time, the function is an object, and the function object can be called without being instantiated if it declares the member Directly. The prototype:http://www.cnblogs.com/yjf512/archive/2011/06/03/2071914.html in JS
Initial understanding of the objectconstructor in Js:
http://www.cnblogs.com/qiantuwuliang/archive/2011/01/08/1930548.htmlJS comes with the map () method
Http://www.cnblogs.com/lengyuehuahun/p/5642807.html
Http://www.cnblogs.com/rocky-fang/p/5756733.html
JS Regular expression
JavaScript RegExp Object RegExp Object
The RegExp object represents a regular expression, which is a powerful tool for performing pattern matching on strings.
RegExp Object Properties
Properties |
Description |
FF |
IE |
Global |
RegExp whether the object has a flag G. |
1 |
4 |
IgnoreCase |
RegExp whether the object has a flag I. |
1 |
4 |
LastIndex |
An integer that indicates the character position at which to start the next Match. |
1 |
4 |
Multiline |
RegExp whether the object has a flag m. |
1 |
4 |
Source |
The source text of the regular Expression. |
1 |
4 |
RegExp Object Methods
Method |
Description |
FF |
IE |
Compile |
Compiles the regular Expression. |
1 |
4 |
Exec |
Retrieves the value specified in the String. Returns the found value and determines its Location. |
1 |
4 |
Test |
Retrieves the value specified in the String. Returns TRUE or False. |
1 |
4 |
Methods for String objects that support regular expressions
Method |
Description |
FF |
IE |
Search |
Retrieves a value that matches a regular expression. |
1 |
4 |
Match |
Finds a match for one or more regular expressions. |
1 |
4 |
Replace |
Replace the substring that matches the regular Expression. |
1 |
4 |
Split |
Splits a string into an array of strings. |
1 |
4 |
JS Regular expression basic usage (classic Full)
Http://www.jb51.net/article/72346.htm
JS Learning Note 1