Results 1 to 6 of 6

Thread: Delete operator javascript

  1. #1
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default Delete operator javascript

    I look this thing up on average maybe once a month (never seem to be sure of the syntax), but never really use it. It's fun in the console, but never seems to do much in real life. Anyone really use this thing in scripts and/or rely upon it much?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  2. #2
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    I've never used it, but I can imagine situations where using the delete operator could be useful.
    Suppose you have an array of elements all of which your want to show on all the pages of your site except on a page called 'delete.html'. On 'delete.html', you don't want to show element[2] (that's me alright). Code could be:
    Code:
    <script>
    var names = ["Beverleyh","John","Arie","Ludwig Van"];
    </script>
    
    <a href="javascript: void()" onclick="if(document.URL.substring(document.URL.lastIndexOf('/')+1, document.URL.length)=='delete.html'){delete names[2]; document.getElementById('names').innerHTML=names}; return false">show celebrities</a>
    
    <div id="names"></div>
    I know that we will have the same result if we have names[2]=undefined instead of delete names[2], but this is only a simple example. If we replace the array of names above with an arry of images in a gallery show, the delete operator will make sure that a certain image will not be displayed on 'delete.html', whereas the use of undefined will replace that image with a 'blank' (I guess).

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I guess I'm just a pack rat. I never want to throw anything away. You have a point, deleting it is perhaps better than having it undefined, except I'm uncertain of the ramifications upon indexing in an array. With an object though, it's the way to go, except it could always be false, whatever, and you could test for that rather than outright deletion. But once an object gets big, it might be better to delete. I like objects because one can do things like:

    Code:
    if((spcld = stg.dates[dn + '']) || (spcld = stg.dates[(mn + 1) + ',' + dn]) || (spcld = stg.dates[yr + ',' + (mn + 1) + ',' + dn])){
    	$(td).addClass('jqspcldate').attr('title', spcld.title || '').click(spcld.click || function(){});
    }
    It's a flexible lookup for (in this case) entries that might have various keys relating to year month and day, or month and day, or day. I often wonder if the lookup takes longer the more entries there are. You do no enumeration or other for i or for p in type of running over the object. It's a lot like if you do if (array[6] && array[6] !== undefined) {whatever;}, but more flexible. Does it matter how large the array or object is in either case? If so, deleting things from the object would be a good idea when they're no longer needed.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I just discovered something pretty cool about delete. You can delete a property that doesn't exist. As long as the object exists, apparently that's OK. Like you can:

    Code:
    delete window.nowaythisisawindowpropertyunlesssomeonesetit
    And even if there is no window.nowaythisisawindowpropertyunlesssomeonesetit, you get no error. That seems handy.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Quote Originally Posted by jscheuer1 View Post
    You have a point, deleting it is perhaps better than having it undefined, except I'm uncertain of the ramifications upon indexing in an array.
    I don't think there will be indexing problems. If we replace the onclick in my code with this:
    Code:
    <a href="javascript: void()" onclick="if(document.URL.substring(document.URL.lastIndexOf('/')+1, document.URL.length)=='delete.html'){delete names[2]; document.getElementById('names').innerHTML=names}; alert(names[3]); return false">show celebrities</a>
    we get Ludwig Van in the alert, which is correct.

    EDIT
    I found this at http://stackoverflow.com/questions/5...in-javascript:
    To remove an element of an array at an index i: array.splice(i, 1).
    If you just want to make the element at index i no longer exist, but you don't want the indexes of the other elements to change: delete array[i].
    Last edited by molendijk; 04-08-2016 at 12:10 PM. Reason: Add info

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Yeah, I hate undefined though when it's still at an index because it can be converted to a string when iterating over the array, and since the indexes in an array (unlike in my test array below) usually mean nothing, I'd rather splice an array. From the console:

    Code:
    var a = [0, 1, 2, 3]
    undefined
    a
    [0, 1, 2, 3]
    delete a[2]
    true
    console.log('A cat ate my homework and ' + a[2]);
     A cat ate my homework and undefined
    undefined
    a[1] || console.log('A cat ate my homework and ' + a[2]);
    1
    a[2] || console.log('A cat ate my homework and ' + a[2]);
     A cat ate my homework and undefined
    With proper testing it's fine. But a major speed advantage in an array is being able to assume everything is defined when iterating over it. Objects are slower because you either have to test each entry as to whether or not it's an 'ownProperty' of the object or just do single item lookups testing each to see if it exists before using it, the latter being the more proper use of an object in my opinion.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Subtraction operator not working!!
    By dcr33 in forum PHP
    Replies: 1
    Last Post: 05-27-2012, 10:00 AM
  2. Replies: 3
    Last Post: 05-28-2009, 10:08 AM
  3. Operator '?'
    By borris83 in forum PHP
    Replies: 5
    Last Post: 04-02-2009, 11:35 AM
  4. C++ Operator Overloading - Reference?
    By magicyte in forum Other
    Replies: 3
    Last Post: 11-26-2008, 07:52 PM
  5. && operator
    By noob_developer in forum JavaScript
    Replies: 1
    Last Post: 05-28-2008, 03:57 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •