Some common JavaScript Functions (json) are described in detail. For details about js learning, refer.
The Code is as follows:
Var YX = {
// Obtain the JS built-in data type. The returned values include [Date, RegExp, Number, String, Array, Boolean, Function, null, undefined, Object].
GetType: function (obj ){
Return obj = null? Obj + "": Object. prototype. toString. call (obj). slice (8,-1 );
}
// Create a simple class
, CreateClass: function (){
Return function (){
This. init. apply (this, arguments );
};
}
// Format the string, YX. format ("{0}, {1}, haha", ["hello", "world"])
, Format: function (str, params ){
Var reg =/{(\ d +)}/g;
Return str. replace (reg, function (match, val ){
Return params [~~ Val];
});
}
// Format the string, YX. format2 ("Mathematics = {mathematics}, Chinese = {language}, haha", {"Mathematics": 100, "Chinese": 99 })
, Format2: function (str, params ){
Var reg =/{([^ {}] +)}/g;
Return str. replace (reg, function (match, val ){
Return params [val];
});
}
// Formatting time, YX. format3 (new Date, "yy-mm-dd H: M: S ")
, Format3: function (date, patten ){
Var y = date. getFullYear (), mon = date. getMonth () + 1, d = date. getDate (), h = date. getHours (), min = date. getMinutes (), s = date. getSeconds ()
, Zero = function (o) {return ("0" + o). slice (-2 )}
, Matchs = {"yy": y, "y" :( y + ""). slice (-2), "mm": zero (mon), "m": mon, "dd": zero (d), "d": d, "HH ": zero (h), "H": h, "MM": zero (min), "M": min, "SS": zero (s), "S ": s };
Return patten. replace (/yy | y | mm | m | dd | d | HH | H | MM | M | SS | S/g, function (match ){
Return matchs [match];
});
}
// Array deduplication, YX. unique ([1, 1, "1", document. body, document. body]
, Unique: function (arr ){
Var kv = {}, len = arr. length, rs = [], t;
For (; len --;){
T = arr [len];
(Kv [t] = undefined | kv [t]! = T) & (kv [t] = t, rs. push (t ));
}
Return rs;
}
};