是否可以在谷歌浏览器的控制台中仅显示用户定义的函数和属性?



在Firebug中,您可以将DOM选项卡的输出设置为仅显示用户定义的函数和属性。这有助于检查是否有对象转义到全局命名空间中。Chrome中有类似的功能吗?

这里有一个近似值:

控制台版本:

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)
      }
    }
  }

书签版本:

javascript:void(function(){for(_ in window) { if (!!window[_] ) { if (/Function/.test(String(window[_].constructor) ) ) { console.log(_) } } }}())

通用版本:

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)
      }
    }
  }
 }

递归版本:

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")

带存储的递归版本:

    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")    

带有取证功能的递归版本:

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") 

参考

  • JavaScript原型和构造函数
  • ECMAScript-5仍然不允许对数组进行子类化

最新更新