对象中的const变量销毁后会发生什么变化? [英] What happens to const variables within an object when it gets destroyed?

查看:48
本文介绍了对象中的const变量销毁后会发生什么变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道忠实地遵循tslint是否可以,因为它建议我在不重新分配时使用const而不是let(这很有意义).尽管我听说另一位开发人员告诉我,使用const不好,因为它无法收集垃圾来清除内存.

I'm just curious to know whether following tslint all the way through faithfully is ok as it advices me to use const instead of let when not reassigned (which makes sense). Although I've heard another developer tell me that using const is bad as it can't get garbage collected to clear memory.

我想更好地了解在对象中使用const的危险.我在下面的示例中担心它是否会导致我的Angular应用程序内发生内存泄漏.

I want to get a better understanding of the dangers of using const within my objects. I'm worried about it in the example below to know if I'm causing memory leaks within my Angular App.

例如在Angular应用中:

E.g. In an Angular app:

import 'rxjs/add/operator/map';

class MyComponentObject {
   constructor(private activatedRoute: ActivatedRoute) {}

   ngOnInit() {
      this.queryParams$ = this.activatedRoute
        .queryParamMap
        .map(params => {

         // is this bad?
         const something = params.get('something') || null;

      })
   }
}

该const仍将保留在销毁对象(ngDestory)上.此类的每次初始化都会以某种方式在一遍又一遍地建立在内存中吗?

Will the const still remain on destroying the object (ngDestory). Will it in some way get built up on memory over and over again with each initialization of this class?

所以通常这样做是明智的选择吗?

So generally is it ok memory wise to do this?

推荐答案

const仅表示您无法重新分配变量.它与垃圾回收无关.

const just means you cannot reassign the variable. It has nothing to do with garbage collection.

在代码片段中,当ngOnInit函数终止时,变量将被销毁.届时,它具有的值将有资格进行垃圾回收(除非在其他地方也引用了该值).

In your snippet the variable will be destroyed when the ngOnInit function terminates. By then the value it had will be eligible for garbage collection (unless it is referenced somewhere else as well).

这篇关于对象中的const变量销毁后会发生什么变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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