Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    187
    Thanks
    5
    Thanked 0 Times in 0 Posts

    jQuery and objects

    Hey,
    I'm coming from a python background and am a noob to js
    I want to create an object that I can reuse. Basically I'm trying to create a list component, that would have some functions built in. So I've done something like this:

    Code:
    component = {
            "delete_items": function () {
                var $items = this.selected_items()
                $items.remove();
            },
            "selected_items": function () {
                return $(".ui-selected", this);
            }
        };
    
    $.extend(component, $("#list_object"));
    What I want to be able, but am stuck on, is dynamically adding a delete button to my list component, so something like:

    Code:
    component={
                "create_button": $("<button class='delete'></button>").click(function(){
                this.delete_items()
            }),
        "delete_items": function () {
    One other question, is there a an equievelant of a python __init__

    Thanks in advance for any help!

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,374
    Thanks
    11
    Thanked 592 Times in 572 Posts
    you can use a constructor function instead of an object literal to initialize specific per-instance valuations.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%


  •  

    Posting Permissions

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