This article mainly introduces the summary of common tips for JavaScript. For more information, see the preface.
I would like to summarize the JavaScript syntax sugar recently introduced and share it with you.
There are detailed descriptions and examples for each piece of sugar, so I will not go into more details.
Accurate type check
The Code is as follows:
/*
* @ Function:
* Type check example
* This method can be used to check whether a variable is of the expected data type.
* @ Params:
* Required variable to be checked for obj
* Config Data Type White List (Optional). The default value is "all ".
* @ Return:
* "True" indicates that the check is successful, and "false" indicates that the check fails.
* @ Examples:
* TypeCheck ("str"); // return true
* TypeCheck ({}, {"[object Array]": 1}); // return false
*/
Function typeCheck (obj, config ){
Var hasOp = Object. prototype. hasOwnProperty,
ToStr = Object. prototype. toString,
_ Config = config | {
"[Object Object]": 1,
"[Object Array]": 1,
"[Object Regex]": 1,
"[Object String]": 1,
"[Object Number]": 1,
"[Object Boolean]": 1,
"[Object Function]": 1,
"[Object Undefined]": 1,
"[Object Null]": 1
};
Return hasOp. call (_ config, toStr. call (obj ));
}
Elegant Prototyping Method
The Code is as follows:
/*
* @ Description:
* Elegant Prototyping Method
* Execute this code snippet in the public scope.
*/
If (typeof Function. prototype. method! = "Function "){
Function. prototype. method = function (name, fn ){
This. prototype [name] = fn;
Return this;
};
}
/*
* Example
*/
// Define a "test class"
Function testFn (){
}
// Add the member method of the test class
TestFn. method ("add", function (a, B ){
Return a + B;
}). Method ("sub", function (a, B ){
Return a-B;
});
// Instantiate
Var testObj = new testFn ();
// Call the member Method
TestObj. add (1, 5); // return 6
TestObj. sub (7,2); // return 5
Quickly create a namespace
The Code is as follows:
/*
* @ Function:
* Create a namespace
* @ Params:
* Ex namespace expression, for example, NSROOT. service. impl
* This expression must start from the root node.
* @ Return:
* The returned Object is the last node of the expression.
* @ Others:
* If you do not like the NSROOT name, simply search and replace it.
*/
Var NSROOT = NSROOT | | {};
NSROOT. namespace = function (ex ){
Var _ ex = ex | "",
NsArray = _ ex. split ("."),
ParentNode = NSROOT,
_ S = "",
I = 0;
// Determine whether the namespace starts from the root node
If (nsArray [0]! = "NSROOT "){
Throw ("The namespace must start from the root node! ");
}
// Remove the root node
NsArray = nsArray. slice (1 );
For (I = 0; I _ S = nsArray [I];
If (parentNode [_ s] === undefined ){
ParentNode [_ s] = {};
}
ParentNode = parentNode [_ s];
}
Return parentNode;
};
/*
* Example
*/
// Create a new namespace
Var impl = NSROOT. namespace ("NSROOT. service. impl ");
Alert (impl = NSROOT. service. impl); // return true
// Create an existing namespace without overwriting the original data
NSROOT. namespace ("NSROOT. service. impl ");
Alert (impl = NSROOT. service. impl); // return true