/*
Function: Modify , so that it can pass parameters and object parameters (also available for setInterval)
How to use: setTimeout(callback function, time, parameter 1,..., parameter n) (FF has been natively supported, but IE does not support it)
*/
var __sto = setTimeout;
= function(callback,timeout,param){
var args = (arguments,2);
var _cb = function(){
(null,args);
}
__sto(_cb,timeout);
}
function aaaaa(a,b,c){
alert(a + b + c);
}
(aaaaa,2000,5,6,7);
/**//*
Function: Understand the return rules of recursive programs (from the inside to the outside)
References between members of objects
*/
var ninja = {
yell: function(n){
return n > 0 ? (n-1) + "a" : "hiy";
}
};
alert((4))//The result is: hiyaaaa;
var samurai = { yell: };
//var ninja = {}; // Comment here or not has an effect on the result
try {
alert((4));
} catch(e){
alert("Uh, this isn't good! Where'd go?" );
}
/** Function: Intercept long strings
* @param {string} str string to be intercepted
* @param {number} size Intercept length (single byte length)
*/
var subStr = function(str, size){
var curSize = 0, arr = [];
for(var i = 0, len = ; i < len; i++){
((i));
if ((i) > 255){
curSize += 2;
if(size === curSize || size === curSize - 1){
return ('');
}
}else{
curSize++;
if(size === curSize){
return ('');
}
}
}
};
var str = '#%*...&#What 1234abcd is not long enough';
alert();
alert((0, 15));
alert(subStr(str, 15));
/**//*
Function: Get the absolute position of the element in the page (relative to the upper left corner of the page)
@param {string} node The DOM element of the position to be requested
*/
function getAbsPosition(node) {
var t = ;
var l = ;
while (node = ) {
t += ;
l += ;
}
alert("top=" + t + "\n" + "left=" + l);
}
/**//*
Function: Statistics and remove duplicate characters
@param str string that needs statistics
Description: It is often used for counting numbers such as duplicate characters in strings, or repeated letters, numbers in arrays, etc.
Here we collect two typical types from the Internet, there are two implementation methods, and there are many other variants, written from different angles, and you can search and learn.
The data to be counted, whether it is an array or a string, just use () or ()
Just convert it to the type required by the function parameter.
*/
// Type 1: Save data with the help of new objects
var count1 = function (str) {
var map = {}, maxCount = 0, maxChar, undefined, i = ;
while (i--) {
var t = (i);
map[t] == undefined ? map[t] = 1 : map[t] += 1;
if (map[t] > maxCount) {
maxChar = t;
maxCount = map[maxChar];
}
}
return "character:" + maxChar + "Count:" + maxCount;
}
function s_0(a) { // The parameter here should be array type
var b = {}, c = [], i;
for (i = 0; i < ; i++){
if (!b[a[i]]) {
c[] = a[i], b[a[i]] = true;
}
}
return c;
}
// Type 2: Regular expression matching statistics
var count2 = function (str) {
var most = ('').sort().join('').match(/(.)\1*/g); //Sort repeat characters
most = (function (a, b) { return - }).pop(); //Sort by frequent occurrence
return + ': ' + most[0];
}
function s_1(a) {
var a = (""), b = [];
while ( > 0)
a = (new RegExp((b[] = (0)), "g"), "");
return b;
}
/**//*
Function: disrupt ordered arrays (generate unordered random arrays)
Note: Everyone should be very clear about the basic sorting algorithm. However, in programming, the opposite operation is often used, that is, randomly disrupting the original ordered array elements.
The following are three methods. The first one is written by me before. Due to the poor level, the time complexity of the code I wrote is too high.
So search for some simple and efficient methods online.
The second type is said to be a "shuffle algorithm", which many people must have heard of;
The third type is to use JS's built-in sort method, which is very simple to implement.
*/
// Method 1 (Learn from the lessons of failure)
function randArray(num) {
var rands = [];
var ra = parseInt(num * ());
(ra);
for (var r = 0; r < num - 1; r++) {
ra = parseInt(num * ());
for (var m = 0; m < ; m++) {
while (rands[m] == ra) {
ra = parseInt(num * ());
m = -1;
}
}
(ra);
}
//alert(rands);
return rands;
}
// Method 2:
//Select random numbers between two [0...) and exchange them as subscripts for the two elements (this is very efficient in order)
/* Note: This is the "shuffle algorithm". Some people have proved that the effect of disruption is as follows:
The effect of random exchange of nums/2 times is very poor, and on average about 1/3 of the objects are still in their original position.
Random exchange of nums is basically available, and on average about 15% of the objects are still in their original location.
Random exchange nums*2 times is really available, and on average about 2% of the objects are still in their original location.
*/
function daluan(nums) {
var array=[];
for (var i = 0; i < nums; i++) {
array[i] = i;
}
for (var i = 0; i < nums; i++) {
var rand = parseInt(nums * ());
var temp = array[i];
array[i] = array[rand];
array[rand] = temp;
}
return array;
}
// Method 3:
// Just let the comparison function randomly pass back -1 or 1 (this is the efficiency of out-of-order)
var testArray3=[1,2,3,4,5,6,7,8,9,10,22,33,55,77,88,99];
(function(){return ()>0.5?-1:1;});
alert(testArray3);