delete和$ destroy之间的区别

时间:2018-05-23 05:24:50

标签: javascript angularjs memory-leaks

我一直在调查angularjs的内存泄漏管理,但我遇到$destroy,但在JavaScript中已经有delete个关键字。两者之间有区别吗?

1 个答案:

答案 0 :(得分:1)

在JavaScript中,delete运算符从对象中删除属性;如果不再保留对同一属性的引用,它最终会自动释放。

var Employee = {
  firstname: "Mohammed",
  lastname: "Haddad"
}

console.log(Employee.firstname);
// expected output: "Mohammed"

delete Employee.firstname;

console.log(Employee.firstname);
// expected output: undefined

在angular,$ scope。$ destroy()中执行它将删除在$ scope上通过$ on注册的所有侦听器。

$scope.$on("$destroy", function() {
});