Welcome to Qi-U Community for programmer and developer-Open, Learning and Share

Categories

0 votes
326 views
in Technique[技术] by (31.9m points)

函数内部引用类型的变量被外部使用,函数是否会释放?

  1. 函数内的变量赋值给外部
var global1
(function() {
    var a = {}
    var b = [1]
    global1 = b
})()
  1. 函数内部的变量返回给外部
var global2
global2 = (function() {
    var a = {}
    var b = [2]
    return b
})()

上述2个例子中的函数是否会释放,函数内部的a b 是否会释放?

另外,是否可以利用Chrome工具查看函数是否会释放?

谢谢大神们~


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (31.9m points)

chrome可以查看调用栈作用域对象:
image.png

变量ab都是函数内的局部变量,保存在栈内的(变量引用指向的值{}[2]保持在堆内的),函数执行完后变量本身就释放了。但是堆中的数据[2]由于被全局变量global1引用着 不会被回收


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Qi-U Community for programmer and developer-Open, Learning and Share

755k questions

547k answers

4 comments

86.4k users

...