Answer the question
In order to leave comments, you need to log in
How to delete an object through a function in JS?
var person = {
'firstName': 'Alex',
'lastName': 'Medvedev',
'getFullName': function() {
return person.firstName + ' ' + this.lastName;
}
}
console.log(person);
// killer functions
var kill = function(obj) { // delete the object - does not work
delete obj;
}
var kill2 = function(obj) { // replace with an empty object - does not work
obj = {};
}
var kill3 = function(obj) { // manually delete each object property - works!
delete obj.firstName;
delete obj.lastName;
delete obj.getFullName;h
}
kill(person);
console.log(person);
kill2(person);
console.log(person);
kill3(person);
console.log(person);
Answer the question
In order to leave comments, you need to log in
var kill = function(obj) { // delete the object - does not work
delete obj;
}
var kill2 = function(obj) { // replace with an empty object - does not work
obj = {};
}
function kill_obj (obj) {
detele window[obj];
}
kill_obj('str_name');
If you just
don't like it, maybe
var kill4 = function (str) {
eval('delete ' + str);
}
kill4('person');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question