简体   繁体   中英

cannot see a variable from Chrome Console but it's accessible in code

Why is a variable accessible from inside a method but not from the browser console?

I have the following typescript code, using angular6, run via ng serve on Windows 10, current Chrome.

import * as d3 from "d3"; // from "npm i d3 --save" version 5.7
// picking a member of d3, nothing magical about "scaleLinear" here
console.log("see", d3.scaleLinear) // it prints ok, ƒ linear() {...

// angular component code fragment below

somefunction() {
    console.log("see again", d3.scaleLinear) // it prints ok, again
    // add a break here
}

I drive the code to that somefunction. At break, in Chrome console when I try:

> console.log(d3.scaleLinear)

it throws an error:

VM2012:1 Uncaught ReferenceError: d3 is not defined
    at eval (eval at push../src/app/user-sel/user-sel.component.ts.UserSelComponent._resyncYSel (user-sel.component.ts:58), <anonymous>:1:13)
    at UserSelComponent.push../src/app/user-sel/user-sel.component.ts.UserSelComponent._resyncYSel (user-sel.component.ts:58)
    at UserSelComponent.push../src/app/user-sel/user-sel.component.ts.UserSelComponent.selYSel (user-sel.component.ts:166)
    at Object.eval [as handleEvent] (UserSelComponent.html:75)
    at handleEvent (core.js:21673)
    at callWithDebugContext (core.js:22767)
    at Object.debugHandleEvent [as handleEvent] (core.js:22470)
    at dispatchEvent (core.js:19122)
    at core.js:19569
    at HTMLSpanElement.<anonymous> (platform-browser.js:993)

How can I access d3 in console directly? What did I miss? I looked at old SO questions on this topic 52332640 but could not find a conclusive answer.

If your pages have any frames, your console could be running in the wrong context. There is a dropdown that will allow you to specify the correct context

For example, in this screenshot, there are two IFrame elements. In order to access variables from scripts running in "contentIframe2" I have to switch the context

控制台上下文

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM