3

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

4

1 回答 1

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

书签版本:

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

参考

于 2013-08-26T21:05:49.580 回答