jcanker Posted February 15, 2011 Share Posted February 15, 2011 New to js here, so please be gentle: Is it possible to delete all properties in one fell swoop? Given: var tempvariable {}; tempvariable.who = "I"; tempvariable.what = "Asked a question"; tempvariable.when = "this morning"; tempvariable.where = "at the best forum board to find answers--phpfreaks.com!"; I understand that to delete one property would be: delete tempvariable.when; but is there an equivalent of delete tempvariable.* ; Or do I have to loop through all the properties and delete them one by one? In short I want to have a function wipe out an existing object (not technically possible since you can't delete a variable in js), so I need to clear it and reinitialize the starting properties when the user selects a different choice from a list. Quote Link to comment https://forums.phpfreaks.com/topic/227754-delete-all-properties-at-once/ Share on other sites More sharing options...
jcanker Posted February 15, 2011 Author Share Posted February 15, 2011 First off, please forgive the lack of headlessCamelCase in the previous example...I know better, I'm just getting used to it... Anyway, will this work? Is there a more efficient way? for (var key in tempvariable) { delete tempvariable[key]; } Quote Link to comment https://forums.phpfreaks.com/topic/227754-delete-all-properties-at-once/#findComment-1174528 Share on other sites More sharing options...
.josh Posted February 17, 2011 Share Posted February 17, 2011 to "wipe" it but not necessarily delete the namespace, you can just do any of these or equivalent tempvariable = ''; tempvariable = null; tempvariable = false; none of these will delete the object per se, but it will overwrite it with a simple variable of whatever value you give it. If you want to completely remove it, do delete window.tempvariable; This is assuming it's a global scoped variable/object, since all global variables/objects are actually within the window object. But overall point is...notice how i'm just using the object name itself, not specifying a property (or method) Quote Link to comment https://forums.phpfreaks.com/topic/227754-delete-all-properties-at-once/#findComment-1175425 Share on other sites More sharing options...
jcanker Posted February 17, 2011 Author Share Posted February 17, 2011 Well, I guess my noob-ness got in the way of using the right vocabulary. I've figured out that I don't want to delete it, because the customer object literal is created at document.ready so that it's available to all the functions. It gets reset inside a function, so if I completely delete it and recreate it because they chose a different customer off the list, then it will only be available to that function. I solved it by adding this at the top of the function that runs when a new customer is selected from the list: //first, clear out any customer/properties that exist: for (var key in customer){delete customer[key];} //next, wipe out the divs that may have been created with the last customer: $('#contact, #tickets, #accounting').empty(); Thank you for answering. I'll probably the delete ability later in this project, so you've addressed it premtively Quote Link to comment https://forums.phpfreaks.com/topic/227754-delete-all-properties-at-once/#findComment-1175688 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.