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,184
    Thanks
    10
    Thanked 569 Times in 550 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/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5


  •  

    Posting Permissions

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