AngularJS Source read one: todebugstring

Source: Internet
Author: User

Brief introduction:

This function returns the debug string:

Number,boolean,string,null,undefined, it will be converted to a string.

Functions with spaces in front of the parentheses will remove the function body, without spaces, and will output all the contents of the function. If you want to get rid of the function body without any spaces, change to/*\{[\s\s]*/

The object is converted to a JSON string, and if attribute a in the object and property B point to the same object, then that property becomes .... If the value is Window,document,scope, the property in the object will be converted to $window, $DOCUMENT, $SCOPE.

It can be found from the source code that json.stringify can turn Null,boolean,number into a string.

Json.stringify's detailed introduction can be seen here: http://www.cnblogs.com/ningvsban/p/3660654.html

functiontodebugstring (obj) {if(typeofobj = = = ' function ') {    //A regular expression that removes the function body, for example: function A () {var i = 0;}, which is filtered into function a ()    //This regular expression is very wonderful, if () there is no space or a number of spaces, it will not remove the function body, change to the following    ///*\{[\s\s]*/    //to match, and what's the use of $?     returnObj.tostring (). replace (/\{[\s\s]*$/, '); }Else if(isundefined (obj)) {return' Undefined '; } Else if(typeofObj!== ' string ') {    returnserializeobject (obj); }  returnobj;}
Test code:
Console.log (todebugstring (1)); Console.log (Todebugstring (true)); Console.log (Todebugstring (Uncle)); Console.log (Todebugstring (NULL));  Console.log (todebugstring (undefined)); Console.log (Todebugstring (function(){    varName = "function with no spaces in front of brackets, preserve function body";  })); Console.log (Todebugstring (functionA () {varName = "function with spaces in front of brackets";  })); varobj = {name: "Uncle"}; varA = todebugstring (NULL); Console.log (todebugstring ({name:"Uncle", Window:window,document:document,height:obj,width:obj}));
Output content:

1
True
Uncle
Null
Undefined
function () {
var name = "Function with no spaces in front of brackets, retain function body";
}
function A ()
{"Name": "Uncle", "window": "$WINDOW", "Document": "$DOCUMENT", "height": {"name": "Uncle"}, "width": "..."}

AngularJS Source read one: todebugstring

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.