简体   繁体   English

为什么在调试器中未定义“ this”,但在console.log中可打印?

[英]Why is 'this' undefined in the debugger but printable in a console.log?

When setting a breakpoint on the console.log statement, why would this be undefined in the debugger but the statement print with no issues? 当在设置断点console.log语句,为什么会this可以在调试器未定义但声明没有问题打印? Am I missing something in regards to scope here? 我在范围方面是否有所遗漏?

export class OptionsSidebarComponent {

  ... 

  public filters: Object = new Object();

  ... 

  public generateFilters() {
    for (let property in this.terms) {
      if (this.terms.hasOwnProperty(property) && this.terms[property] !== null) {

        if (!this.filters.hasOwnProperty(this.productGroup.id)){
          this.filters[this.productGroup.id] = new Filters();
        }

        this.terms[property].forEach((term) => {
          console.log(this.filters);
        });
      }
    }
  }
}

With typescript While debugging, keep in mind that transpilers can rename variables. 使用typescript调试时,请记住,编译器可以重命名变量。 Using the original name in the console without sourcemaps that include renaming will show you undefined even if the original value isn't. 在控制台中使用不带重命名的源映射的原始名称将显示未定义,即使原始值不是。 Make sure you use the transpiled name in watches or console commands. 确保在监视或控制台命令中使用已转换的名称。

When you're referencing this with your console.log statement inside its own class, you're using it in its relevant scope. 如果引用this与自己的类里面你的console.log语句,你在它的相关范围使用。 Depending on the framework you are using, different terms are used to reference your class... AngularJS used $scope - Angular 2+ uses this to reference the current class (or current scope/context). 根据您使用的框架,使用不同的术语来引用您的类... AngularJS使用了$scope -Angular 2+使用this来引用当前的类(或当前的作用域/上下文)。

When you use this in your debugger you're using it outside of its scope. 在调试器中使用this ,您将在其范围之外使用它。 It no longer references the class you intend it to. 它不再引用您想要的类。

Each one of your components in Angular uses this to reference itself. Angular中的每个组件都使用this引用自己。 Here's an article to explain in further detail: https://angular-2-training-book.rangle.io/handout/features/refresher_on_this.html 这是一篇文章,将进行详细说明: https : //angular-2-training-book.rangle.io/handout/features/refresher_on_this.html

If we simplify it to basic javascript and look at your class as just a function a good example would be this: 如果我们将其简化为基本的javascript并将您的类视为一个函数,那么一个很好的例子就是:

function example(){
    var anything = 10;
    console.log(anything); //output: 10
}

console.log(anything); //undefined! we're out of scope :)

So looking at it from this perspective, this is nothing magical. 因此,从这个角度来看, this并不是什么神奇的事情。 It's just provided to us by Angular to make our references to other things inside our components easier. 它只是由Angular提供给我们的,目的是使我们对组件内部其他内容的引用更加容易。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 XMLHttpRequest 对象的属性只能通过 console.log() 打印? - Why properties of an XMLHttpRequest object are only printable through console.log()? 为什么当我尝试编辑它时该元素在表单中返回未定义,但在调试器和 console.log 中正确显示? - Why is this element returning undefined in the form when I attempt to edit it, but displaying correctly in debugger and console.log? 为什么console.log函数返回undefined? - Why console.log function returns undefined? 为什么 console.log([1, 2, 3, 4, 5, 6, 7, 8, 9, 10][1, 2, 3, 4, 5, 6, 7][1, 2, 3]) 日志不明确的? - Why console.log([1, 2, 3, 4, 5, 6, 7, 8, 9, 10][1, 2, 3, 4, 5, 6, 7][1, 2, 3]) logs undefined? 为什么此代码中的console.log返回undefined? - Why console.log in this code return undefined? 为什么console.log(fibonacci(3)); 打印未定义? - Why console.log(fibonacci(3)); print undefined? Console.log 未定义 - Console.log is undefined 为什么console.log()和调试器中的对象值不同? - Why object values are different in console.log() and in debugger? 调试器检查的代码在console.log中显示正确的返回值,那么为什么我返回undefined? - Debugger-checked code shows correct return value in console.log, so why am I returning undefined? console.log(String(console.log('Not undefined'))==='未定义'); console.log(String(console.log('Not undefined'))!=='Not undefined'); - console.log(String(console.log('Not undefined')) === 'undefined'); console.log(String(console.log('Not undefined')) !== 'Not undefined');
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM