JavaScript Common tips _javascript Tips

Source: Internet
Author: User
Tags object object

Objective

Summarize the recent contact with JavaScript grammar candy, and share with you.

Each piece of sugar has a detailed description and examples, do not say more.

Accurate type checking

Copy Code code as follows:

/*
* @function:
* Type Check Example
* This method allows you to check whether a variable is the desired data type
* @params:
* obj needs to check the variable, required
* Config data type white list, optional, default to all types
* @return:
* True indicates that the check passed, false failed
* @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 method of adding prototypes

Copy Code code as follows:

/*
* @description:
* Elegant method of adding prototypes
* Execute this code fragment in a public scope
*/
if (typeof Function.prototype.method!== "Function") {
Function.prototype.method = function (NAME,FN) {
This.prototype[name] = fn;
return this;
};
}
/*
* Use examples
*/
Define a "test class"
function Testfn () {
}
To add a member method for a test class
Testfn.method ("Add", function (a,b) {
return a + B;
). Method ("Sub", function (a,b) {
return a-b;
});
Instantiation of
var testobj = new Testfn ();
Calling member Methods
Testobj.add (1,5); Return 6
Testobj.sub (7,2); Return 5

Quick Create namespaces

Copy Code code as follows:

/*
* @function:
* Create namespaces
* @params:
* Ex-namespace expressions, for example: NSROOT.service.impl
* This expression must be written from the root node
* @return:
* Returns object, which is the last node of an expression
* @others:
* If you do not like Nsroot this name, simple find replacement can
*/
var nsroot = Nsroot | | {};
Nsroot.namespace = function (ex) {
var _ex = ex | | "",
Nsarray = _ex.split ("."),
ParentNode = Nsroot,
_s = "",
i = 0;
To determine whether a namespace starts at the root node
if (Nsarray[0]!== "Nsroot") {
Throw (the namespace must start from the root node!) ");
}
Remove root node
Nsarray = Nsarray.slice (1);
for (i = 0;i<nsarray.length;i++) {
_s = Nsarray[i];
if (parentnode[_s] = = undefined) {
Parentnode[_s] = {};
}
ParentNode = parentnode[_s];
}
return parentnode;
};
/*
* Use examples
*/
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.