javascript - how to keep value of variable after making it null -
i know sounds silly asking, there way of keeping value of b
variable after delete a
variable?
var = 'some string or number'; var b = a; delete a; b still = 'some string or number';
or other way keep b
value?
delete
not delete values or variables, deletes properties on object (mdn: https://developer.mozilla.org/en-us/docs/web/javascript/reference/operators/delete).
for instance:
var myobj = { a: 1, b: 2 }; delete myobj.a; // myobj === { b: 2 }
of course can keep reference value in variable: var copy = myobj.a
(before delete property). variable hold value (for primitives basic numbers, strings, booleans) or reference (for objects etc.). either way, copy hold value still after original property has been deleted:
var obj = { a: "hello" }; var copy = obj.a; delete obj.a; console.log(copy); // "hello"
Comments
Post a Comment