dop251/goja

Does setting a variable to nil free up the space?

chenjink674 opened this issue · 1 comments

func TestDeleteFunction(t *testing.T) {
vm := goja.New()
script := let foo = 'bar'; let obj = { name: 'Alice', age: 25 };
_, err := vm.RunString(script)
if err != nil {
t.Fatal(err)
}

// it doesn't work
if err := vm.GlobalObject().Delete("obj"); err != nil {
	t.Log(err)
	return
}

obj := vm.Get("obj")
if obj == nil {
	t.Log("obj is nil")
	return
} else {
	t.Log(obj) //  can't delete "obj", output : [object Object]
}

// maybe it's worked ????
if err := vm.Set("foo", nil); err != nil {
	t.Log(err)
	return
}
foo := vm.Get("foo")
if foo == nil {
	t.Log("foo is nil")
} else {
	t.Log(foo) //  ouput: null 
}

}

dop251 commented

Lexical declarations (i.e. let and const) do not become properties of the global object (unlike var declarations). There is no way in ECMAScript standard to "undeclare" a lexical declaration, but setting it to nil, or any other value, drops the reference to the old value.