JavaScript gets a code instance of the function name, function parameter, object property name-Basics

Source: Internet
Author: User

3 ways to get function names

Example 1:

A way to see in the JS authoritative guide:

Function.prototype.getName = function () {
return THIS.name | | This.tostring (). Match (/function\s* ([^ (]*) \ (/) [1]
}

Example 2:

If the current function is a well-known function, it returns its name, and if it is an anonymous function, it returns the function variable name of the assignment, and returns "Anonymous" if the anonymous function is in the closure.

Copy Code code as follows:

var getfnname = function (callee) {
var _callee = callee.tostring (). replace (/[\s\?] */g, ""),
Comb = _callee.length >= 50? : _callee.length;
_callee = _callee.substring (0,comb);
var name = _callee.match (/^function ([^\ (]+?) \(/);
if (name && name[1]) {
return name[1];
}
var caller = Callee.caller,
_caller = caller.tostring (). replace (/[\s\?] */g, "");
var last = _caller.indexof (_callee),
str = _caller.substring (last-30,last);
Name = Str.match (/var ([^\=]+?) \=/);
if (name && name[1]) {
return name[1];
}
Return "Anonymous"
};

Use: Executes this function inside the function to be investigated, passing in a parameter for Arguments.callee.
Copy Code code as follows:

function ee () {
//+++++++++++++++++++++++++++++++++
var fnname =getfnname (Arguments.callee)
//+++++++++++++++++++++++++++++++++
Alert (FnName)
};
EE ();

Example 3:

Copy Code code as follows:

function GetFuncName (_callee)
{
var _text = _callee.tostring ();
var _scriptarr = document.scripts;
for (var i=0; i<_scriptarr.length; i++)
{
var _start = _scriptarr[i].text.indexof (_text);
if (_start!=-1)
{
if (/^function\s*\ (. *\). *\r\n/.test (_text))
{
var _temparr = _scriptarr[i].text.substr (0, _start). Split (' \ r \ n ');
Return _temparr[_temparr.length-1].replace (/(VAR) | ( \s*)/g, '). Replace (/=/g, ");
}
Else
Return _text.match (/^function\s* ([^\ (]+). *\r\n/) [1];
}
}
}
function A ()
{
Return GetFuncName (Arguments.callee);
}
var B = function ()
{
Return GetFuncName (Arguments.callee);
}
Window.alert (A ());
Window.alert (b ());

The above method also has one situation cannot solve, hoped that has the way to give the guidance.

Copy Code code as follows:

var x =
{
Run:function ()
{
Return GetFuncName (Arguments.callee);
}
}
Window.alert (X.run ());

The name of the function cannot be obtained in this case;

Second, JS get all the parameters of the function and traverse an object all the property names and values of methods

1. Get all parameters

Copy Code code as follows:

function Test () {

for (Var i=0;i<arguments.length;i++)
document.write (Arguments[i]);

}

2. The method of traversing all the property names and values of an object

Copy Code code as follows:

<script language= "JavaScript" >

var obj = new Object ();
Obj.myname = "I am the object";
Obj.pro2 = "23";
Obj.pro3 = "Abcdeg"; PHP Programmer Station

for (items in obj) {
document.write ("Property: +items+" value is ("+ Obj[items] +") ");
document.write ("<br>");
}
</script>


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.