使用Chrome DevTools控制台以编程方式访问功能范围 [英] Programmatically accessing function scope using Chrome DevTools console

查看:191
本文介绍了使用Chrome DevTools控制台以编程方式访问功能范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我打开Chrome(v35)DevTools并检查一个对象时,控制台可以显示嵌套在函数中的东西,包括标记为函数作用域的东西。



例如,在看stackoverflow.com时,我可以看到有一个全局的 $ 对象包含另一个名为 Callbacks 回调 $ 一样,具有包含 Closure 和全球






  • 问题1:有什么区别一些命名对象直接嵌套在一个函数中,另一个对象包含在函数作用域中的Closure中?

  • 问题2:如何以编程方式引用函数作用域控制台? 窗口。$。回调。??? chrome.function ???(window。$。Callbacks)



我想问的原因是因为我正在寻找内存泄漏,并希望根据对象类型和属性名称来搜索函数闭包中保存的对象。

解决方案

直接嵌套在函数中的某个命名对象和包含在函数作用域中的Closure内的某个对象之间有什么区别?




直接与函数嵌套的对象是函数对象的一个​​属性。例如, $。Callback 具有值 1 .length 属性$ c>,它有一个 .prototype 属性,它从 __ proto __ ) > Function.prototype 等。



范围中的对象是一个变量,可以从围绕该函数的范围访问。请参阅 JavaScript关闭如何工作?


如何以编程方式在控制台中引用函数作用域?

你不能。范围不可编程访问。我不认为devtools有任何帮手来允许这个。另请参阅如何在Google Chrome开发人员工具中搜索范围变量?


When I open up Chrome (v35) DevTools and inspect an object, the console can show me things nested within functions, including something labeled as the '"function scope".

For example, when looking at stackoverflow.com, I can see that there's a global $ object containing another function called Callbacks. Callbacks, as does $, has a functional scope containing Closure and Global.

  • Question 1: What is the difference between some named object nested directly within a function and some object contained within a Closure in its function scope?
  • Question 2: How do I programmatically reference a function scope in the console? window.$.Callbacks.???? chrome.function???(window.$.Callbacks)

The reason I ask is because I'm looking for memory leaks and would like to search the objects held within functions' closures based on object types and property names.

解决方案

What is the difference between some named object nested directly within a function and some object contained within a Closure in its function scope?

The object nested directly with the function is a property of the function object. For example, $.Callback has a .length property with value 1, it does have a .prototype property, it does inherit (__proto__) from Function.prototype etc.

The object in the scope is a variable that is accessible from the scope that surrounds the function. See How do JavaScript closures work?

How do I programmatically reference a function scope in the console?

You cannot. Scopes are not programatically accessible. I don't think the devtools have any helpers to allow this either. See also How do I search through scope variables in Google Chrome Developer Tools?

这篇关于使用Chrome DevTools控制台以编程方式访问功能范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆