Javascript code example for obtaining function names, function parameters, and Object Property names

Source: Internet
Author: User

I. Three methods for obtaining function names

Instance 1:

One method seen in the js authoritative guide:

Function. prototype. getName = function (){
Return this. name | this. toString (). match (/function \ s * ([^ (] *) \ (/) [1]
}

Instance 2:

If the current function is a famous function, its name is returned. If it is an anonymous function, the variable name of the function to be assigned is returned. If it is an anonymous function in the closure, "anonymous" is returned ".
Copy codeThe Code is as follows:
Var getFnName = function (callee ){
Var _ callee = callee. toString (). replace (/[\ s \?] */G ,""),
Comb = _ callee. length> = 50? 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: execute this function within the function to be investigated and input a parameter, arguments. callee.
Copy codeThe Code is as follows:
Function ee (){
// ++
Var fnname = getFnName (arguments. callee)
// ++
Alert (fnname)
};
Ee ();

Instance 3:

Copy codeThe Code is 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 ()
{
Return getFuncName (arguments. callee );
}
Var B = function ()
{
Return getFuncName (arguments. callee );
}
Window. alert (());
Window. alert (B ());

The above method still has a problem that cannot be solved. I hope you can give some advice.
Copy codeThe Code is as follows:
Var x =
{
Run: function ()
{
Return getFuncName (arguments. callee );
}
}
Window. alert (x. run ());

In this case, the function name cannot be obtained;

Ii. How to obtain all function parameters and traverse all attribute names and values of an object in js

1. Get all parameters
Copy codeThe Code is as follows:
Function test (){

For (var I = 0; I <arguments. length; I ++)
Document. write (arguments [I]);

}

2. Method for traversing all attribute names and values of an object
Copy codeThe Code is as follows:
<Script language = "javascript">

Var obj = new Object ();
Obj. myname = "I am an object ";
Obj. pro2 = "23 ";
Obj. pro3 = "abcdeg"; php programmer site

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.