是否可以仅显示用户定义的功能& Google Chrome控制台中的属性?

时间:2013-02-20 10:44:34

标签: javascript algorithm function google-chrome-devtools host-object

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

1 个答案:

答案 0 :(得分:0)

以下是近似值:

控制台版本:

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版本:

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

<强>参考