问题
In Firebug you can set the output of the DOM tab to only show user defined functions and properties. This is helpful for checking if you have objects escaping into the global namespace. Is there an equivalent in Chrome?
回答1:
Here's a close approximation:
Console version:
var current;
for(current in window)
{
/* If the property is not null or undefined */
if (!!window[current] )
{
/* If the constructor is the Function object */
if (/Function/.test(String(window[current].constructor) ) )
{
/* Print to the console */
console.log(current)
}
}
}
Bookmarklet version:
javascript:void(function(){for(_ in window) { if (!!window[_] ) { if (/Function/.test(String(window[_].constructor) ) ) { console.log(_) } } }}())
Generic version:
function getUDFs()
{
var current;
/* Use current instead of _ to avoid creating an iterator global variable */
for(current in arguments[0])
{
/* If the property is not null or undefined */
if (!!arguments[0][current] )
{
/* If the constructor is the Function object */
if (/Function/.test(String(arguments[0][current].constructor) ) )
{
/* Print to the console */
console.log(current)
}
}
}
}
Recursive version:
function getUDFs()
{
var current;
/* Use current instead of _ to avoid creating an iterator global variable */
for(current in arguments[0])
{
getUDFs.id = arguments[1] + " => ";
/* If the property is not null or undefined */
if (!!arguments[0][current] )
{
/* If the constructor is the Function object */
if (/Function/.test(String(arguments[0][current].constructor) ) )
{
/* Print to the console */
console.log(getUDFs.id + current)
}
/* Check object properties */
if (/Object/.test(String(arguments[0][current].constructor) ) )
{
getUDFs(arguments[0][current], getUDFs.id + current)
}
/* Check prototype properties, but skip constructor prototypes */
if (!!arguments[0][current] && arguments[0][current].hasOwnProperty("prototype") && arguments[0][current] !== arguments[0]["constructor"])
{
getUDFs(arguments[0][current]["prototype"], getUDFs.id + current + " => prototype")
}
}
}
}
getUDFs(jQuery,"jQuery")
Recursive version with storage:
function getUDFs()
{
var current;
/* Use current instead of _ to avoid creating an iterator global variable */
for(current in arguments[0])
{
getUDFs.id = arguments[1] + " => ";
/* If the property is not null or undefined */
if (!!arguments[0][current] )
{
/* If the constructor is the Function object */
if (/Function/.test(String(arguments[0][current].constructor) ) )
{
/* Store in an array */
if (getUDFs.hasOwnProperty("data") )
{
getUDFs.data.push(getUDFs.id + current)
}
else
{
getUDFs.data = []
}
}
if (/Object/.test(String(arguments[0][current].constructor) ) )
{
getUDFs(arguments[0][current], getUDFs.id + current)
}
}
}
}
getUDFs(jQuery,"jQuery")
Recursive version with forensics:
function getUDFs()
{
var current;
/* Use current instead of _ to avoid creating an iterator global variable */
for(current in arguments[0])
{
getUDFs.id = arguments[1] + " => ";
/* If the property is not null or undefined */
if (!!arguments[0][current] )
{
/* If the constructor is the Function object */
if (/Function/.test(String(arguments[0][current].constructor) ) )
{
/* Store in an array */
if (getUDFs.hasOwnProperty("data") )
{
try{getUDFs.data.push(getUDFs.id + current + String().concat("- args: ","(", arguments[0][current]["length"], ")"))}catch(e){getUDFs.data.push(getUDFs.id + current)};
try{getUDFs.data[getUDFs.data.length-1] += "required:" + !arguments[0][current]() ? true: false}catch(e){getUDFs.data[getUDFs.data.length-1] += "required: true"}
}
else
{
getUDFs.data = []
}
}
if (arguments[0].hasOwnProperty(current) )
{
if (/Object/.test(String(arguments[0][current].constructor) ) )
{
getUDFs(arguments[0][current], getUDFs.id + current)
}
}
}
}
}
getUDFs(jQuery,"jQuery");
getUDFs.data.toString().replace(",","\n","g")
References
- JavaScript Prototypes and Constructors
- How ECMAScript-5 Still does not allow to subclass an array
来源:https://stackoverflow.com/questions/14977889/is-it-possible-to-show-only-user-defined-functions-properties-in-google-chrome