This article mainly records several common javascript code segments that I have collected, which are usually used in projects. If you need them, refer to section 1. Converting json into strings.
The Code is as follows:
Function json2str (o ){
Var arr = [];
Var fmt = function (s ){
If (typeof s = 'object' & s! = Null) return json2str (s );
Return/^ (string | number) $/. test (typeof s )? "'" + S + "'": s;
};
For (var I in o) arr. push ("'" + I + "':" + fmt (o [I]);
Return '{' + arr. join (',') + '}';
}
2. Convert the timestamp to Date
The Code is as follows:
Function fromUnixTime (timeStamp ){
If (! TimeStamp | timeStamp <1000 | timeStamp = '') return "";
Var theDate = new Date (parseInt (timeStamp) * 1000 );
Return theDate;
}
3. Data-format
The Code is as follows:
// Prepared by: meizz
// Extend the Date to convert the Date to a String in the specified format
// One to two placeholders can be used for month (M), Day (d), hour (h), minute (m), second (s), and quarter (q,
// Year (y) can use 1-4 placeholders, Millisecond (S) can only use 1 placeholder (1-3 digits)
// Example:
// (New Date (). Format ("yyyy-MM-dd hh: mm: ss. S") => 08:12:04. 423
// (New Date (). Format ("yyyy-M-d h: m: s. S") => 2012-12-02. 18
Date. prototype. Format = function (fmt ){
Var o = {
"M +": this. getMonth () + 1, // month
"D +": this. getDate (), // day
"H +": this. getHours (), // hour
"M +": this. getMinutes (), // minute
"S +": this. getSeconds (), // second
"Q +": Math. floor (this. getMonth () + 3)/3), // quarter
"S": this. getMilliseconds () // millisecond
};
If (/(y +)/. test (fmt ))
Fmt = fmt. replace (RegExp. $1, (this. getFullYear () + ""). substr (4-RegExp. $1. length ));
For (var k in o)
If (new RegExp ("(" + k + ")"). test (fmt ))
Fmt = fmt. replace (RegExp. $1, (RegExp. $1. length = 1 )? (O [k]): ("00" + o [k]). substr ("" + o [k]). length )));
Return fmt;
};
4. Add n days to the date
The Code is as follows:
Function addDay (number ){
Return fromUnixTime (new Date (). getTime ()/1000 + 24*60*60 * number );
}
5. Mutual calls between the parent and child forms when iframe is used
The Code is as follows:
// The parent form calls the function in the child form
Window. frames ['ifm _ id']. valueChange ("id_101 ");
// The child form calls the function of the parent form
Parent. refreshTree ("nodeId_202 ");
6. Pop-up forms and return values
The Code is as follows:
// Pop-up form
Var url = "http://www.baidu.com ";
Win = window. showModalDialog (url, window, "dialogLeft: 400; dialogTop: 200; dialogWidth: 560px; dialogHeight: pixel PX; scroll: yes; menubar: no; toolbar: no; status: no; ");
// Set the return value in the pop-up form
Var result = new Array ();
Result [0] = "id_101 ";
Result [1] = "name_202 ";
Window. returnValue = result;
Window. close ();
7. javascript scope [only global scope and function scope, javascript does not have block Scope]
The Code is as follows:
// 1. Global Scope
Var id = "global variable"; // 1.1 variables defined outside the Function
Function showMsg (){
Message = "global message"; // 1.2 variables that are directly assigned an undefined Value
// It is defined as a global variable when used for the first time.
}
// 2. Function Scope
Function doCheck (){
Var data = "function data"; // 2.1 variables defined within the function
}
8. javascript Inheritance Mechanism
The Code is as follows:
// 1. Object impersonate inheritance
Function Person (strName ){
// Private fields
Var name = strName;
// Public methods
This. getName = function (){
Return name;
};
}
Function Student (strName, strSchool ){
// Define attributes and methods of the parent class
This. parent = Person;
This. parent (strName );
Delete this. parent; // delete the Temporary Variable parent.
// Define new attributes and Methods
// Private fields
Var school = strSchool;
// Public methods
This. getSchool = function (){
Return school;
};
}
// 2. The call (..) or apply (..) Inheritance of the Funtion object
// The difference between call and apply is:
// The second parameter of call is a variable parameter;
// The second parameter of apply is Array;
Function Animal (strName, intAge ){
// Private fields
Var name = strName;
Var age = intAge;
// Public methods
This. getName = function (){
Return name;
};
This. getAge = function (){
Return age;
};
}
Function Cat (strName, intAge, strColor ){
// Define attributes and methods of the parent class
Animal. call (this, strName, intAge );
// Animal. apply (this, new Array (strName, intAge ));
// Define new attributes and Methods
// Private fields
Var color = strColor;
// Public methods
This. getInfo = function (){
Return "name:" + this. getName () + "\ n"
+ "Age:" + this. getAge () + "\ n"
+ "Color:" + color;
};
}
// 3. prototype inheritance
// Attributes and Methods declared by prototype are shared by all objects
// Prototype is only used when reading attributes
Function. prototype. extend = function (superClass ){
// Here F is used to prevent the subclass from accessing the attribute this. xxx in the parent class
Function F (){};
F. prototype = superClass. prototype;
// Parent Constructor
This. superConstructor = superClass;
This. superClass = superClass. prototype;
This. prototype = new F ();
This. prototype. constructor = this;
};
Function. prototype. mixin = function (props ){
For (var p in props ){
This. prototype [p] = props [p];
}
};
Function Box (){}
Box. prototype = {
GetText: function (){
Return this. text;
},
SetText: function (text ){
This. text = text;
}
};
Function CheckBox (){}
CheckBox. extend (Box );
CheckBox. mixin ({
IsChecked: function (){
Return this. checked;
},
SetChecked: function (checked ){
This. checked = checked;
}
});
9. call, apply & bind
The Code is as follows:
// ThisArg indicates the object indicated by this during fun
// Call & apply will immediately execute fun and return results
Var result = fun. call (thisArg, arg1 ,...);
Var result = fun. apply (thisArg, [argsArray]);
// ThisArg indicates the object indicated by this during fun
// Bind returns an anonymous function.
Var tmpfun = fun. bind (thisArg );
Var result = tmpfun (arg1 ,...);
The Code is as follows: