Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7902045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:23:37+00:00 2026-06-03T09:23:37+00:00

I have a jQuery UI function, inside the _create function I have var self

  • 0

I have a jQuery UI function, inside the _create function I have var self = this so I can access the plugin context from other functions in the plugin.

I also have this code inside the _create function:

//setup sortable handler
if (self.options.sortable !== false) {

     var soptions = {
         items:'.tagit-choice',
         containment:'parent',
         start:function (event, ui) {
             console.log('sorting started', event, ui);
             self._sortable.tag = $(ui.item);
             self._sortable.origIndex = self._sortable.tag.index();
         },
         update:function (event, ui) {
             console.log('sorting started', event, ui);
             self._sortable.newIndex = self._sortable.tag.index();
             self._moveTag(self._sortable.origIndex, self._sortable.newIndex);
             console.log('moved tag', self._sortable.origIndex, 'to', self._sortable.newIndex);

         }
     };

     if (self.options.sortable == 'handle') {
         $('.tagit-choice', this.element).prepend('<a class="ui-icon ui-icon-grip-dotted-vertical" style="float:left"></a>');
         soptions.handle = 'a.ui-icon';
         soptions.cursor = 'move';
     }

     self.element.sortable(soptions);
 }

To setup sortable inside the plugin.

It has a call to self._moveTag where inside I am calling console.log(self) which I expect self to be the plugin instance, but instead it is the DOMWindow. Why is this?

_moveTag: function (old_index, new_index) {
    console.log(self);
    self.tagsArray.splice(new_index, 0, self.tagsArray.splice(old_index, 1)[0]);
    for (var ind in self.tagsArray)
        self.tagsArray[ind].index = ind;

    if(self.options.select){
        $('option:eq(' + old_index + ')', this.select).insertBefore($('option:eq(' + new_index + ')', this.select));
    }
},

Hopefully this example is clearer:

(function($) {

    $.widget("ui.widgetName", {

        options: {
            sortable: true
        },

        _sortable: {},

        _create: function() {

            var self = this;

            if (self.options.sortable !== false) {
                self.element.sortable({
                    items: '.tagit-choice',
                    containment: 'parent',
                    start: function(event, ui) {
                        self._sortable.tag = $(ui.item);
                        self._sortable.origIndex = self._sortable.tag.index();
                    },
                    update: function(event, ui) {
                        self._sortable.newIndex = self._sortable.tag.index();
                        self._moveTag(self._sortable.origIndex, self._sortable.newIndex);
                    }
                });
            }
        },

        _moveTag: function(old_index, new_index) {
            console.log(self); // <-- this returns DOMWindow?!?!?
        }
    });
})(jQuery);​
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:23:38+00:00Added an answer on June 3, 2026 at 9:23 am

    Why would you expect self to be defined at all inside _moveTag? Apparently someone is leaking a self into window.self and you are picking up the bad self inside _moveTag and that self happens to be window; given something like this:

    (function($) {
        $.widget('ui.pancakes', {
            _create: function() {
                var self = this;
                self._moveTag();
            },
            _moveTag: function() {
                // ...
            }
        });
    })(jQuery);
    

    The self inside _create is the widget you want your self to be but that self is not in scope for _moveTag, _moveTag is picking up the leaked window.self from somewhere else. So you can use self inside _create and any functions defined within _create and you get what expect; but _moveTag is defined as a widget method in the usual way (rather than inside _create‘s scope) so it picks up the bad outer self and that self is window.

    I think your private _moveTags method is guilty of self abuse. You should be using this inside widget methods and make sure the caller uses the correct context; jQuery-UI will call public methods with the right context for you and you should be able to handle private methods (such as _moveTag) quite easily on your own.

    Here’s a quick demo for illustration: http://jsfiddle.net/ambiguous/UhE3F/


    A little investigation reveals a shocking fact: window is supposed to have a self property:

    window.self

    Returns an object reference to the window object.

    and it turns out that the var self = this convention is one of the worst ideas ever or at least using the name self for this is an awful idea. What a lovely source of confusing bugs this is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery json ajax call and inside my success function I have
I've been writing some jQuery functions that have JavaScript variables and looping, etc inside
Here's the story... I have a jQuery function that does something, this function is
I have function: function get_playlist(){ var result = jQuery.ajax({ url: '<?php echo admin_url('admin-ajax.php'); ?>',
Assuming I have a JQuery object stored in $e obtained through this selector: var
I have a jQuery function already to perform the task I need but is
I have the following jquery function for filtering the contents of a listbox on
I have a jQuery setInterval function named timerIncrement that times out (stops incrementing the
I have the following jquery function > <script type=text/javascript> > > $(document).ready(function() { >
I have the following jQuery function: $(function(){ if ($(.menu li).hasClass(active)) { $(.active).css(margin-top, 6px); }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.