Note that there are some explanatory texts on larger screens.

plurals
  1. POHow this and $(this) end up being the same when extending jQuery?
    primarykey
    data
    text
    <p>I'm trying to work with a plugin which extends jQuery like so:</p> <pre><code> $.extend({ StatelessDeferred: function () { var doneList = $.Callbacks("memory"), promise = { done: doneList.add, // Get a promise for this deferred // If obj is provided, the promise aspect is added to the object promise: function (obj) { var i, keys = ['done', 'promise']; if (obj === undefined) { obj = promise; } else { for (i = 0; i &lt; keys.length; i += 1) { obj[keys[i]] = promise[keys[i]]; } } return obj; } }, deferred = promise.promise({}); deferred.resolveWith = doneList.fireWith; return deferred; } }); </code></pre> <p>Problem is (and I'm not even sure it's caused here), after the callback loads, inside a <code>done</code> callback, both <code>this</code> and <code>$(this)</code> are the same, so I end up for example with: <code>this === $(this) === $(document)</code>.</p> <p>I'm not really sure I understand what's being extended. The plugin works fine with it except for the false assignment.</p> <p><strong>Question:</strong><br> Could the above extension be causing <code>this === $(this) === $(document)</code>? </p> <p><strong>EDIT</strong>: Full plugin (120lines):</p> <pre><code>"use strict"; (function (window, $) { $.extend({ StatelessDeferred: function () { var doneList = $.Callbacks("memory"), promise = { done: doneList.add, // Get a promise for this deferred // If obj is provided, the promise aspect is added to the object promise: function (obj) { var i, keys = ['done', 'promise']; if (obj === undefined) { obj = promise; } else { for (i = 0; i &lt; keys.length; i += 1) { obj[keys[i]] = promise[keys[i]]; } } return obj; } }, deferred = promise.promise({}); deferred.resolveWith = doneList.fireWith; // All done! return deferred; } }); var routes = [], current_priority = 0, methods = { add: function (pattern, priority) { var i = 0, inserted = false, length = routes.length, dfr = $.StatelessDeferred(), context = $(this), escapepattern, matchingpattern; if (priority === undefined) { priority = 0; } if (pattern !== undefined) { // http://simonwillison.net/2006/Jan/20/escape/ escapepattern = pattern.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&amp;"); matchingpattern = escapepattern .replace(/&lt;int:\w+&gt;/g, "(\\d+)") .replace(/&lt;path:\w+&gt;/g, "(.+)") .replace(/&lt;\w+&gt;/g, "([^/]+)"); while (!inserted) { if ((i === length) || (priority &gt;= routes[i][2])) { routes.splice(i, 0, [new RegExp('^' + matchingpattern + '$'), dfr, priority, context]); inserted = true; } else { i += 1; } } } return dfr.promise(); }, go: function (path, min_priority) { var dfr = $.Deferred(), context = $(this), result; if (min_priority === undefined) { min_priority = 0; } setTimeout(function () { var i = 0, found = false, slice_index = -1, slice_priority = -1; for (i = 0; i &lt; routes.length; i += 1) { if (slice_priority !== routes[i][2]) { slice_priority = routes[i][2]; slice_index = i; } if (routes[i][2] &lt; min_priority) { break; } else if (routes[i][0].test(path)) { result = routes[i][0].exec(path); dfr = routes[i][1]; context = routes[i][3]; current_priority = routes[i][2]; found = true; break; } } if (i === routes.length) { slice_index = i; } if (slice_index &gt; -1) { routes = routes.slice(slice_index); } if (found) { dfr.resolveWith( context, result.slice(1) ); } else { dfr.rejectWith(context); } }); return dfr.promise(); }, }; $.routereset = function () { routes = []; current_priority = 0; }; $.routepriority = function () { return current_priority; }; $.fn.route = function (method) { var result; if (methods.hasOwnProperty(method)) { result = methods[method].apply( this, Array.prototype.slice.call(arguments, 1) ); } else { $.error('Method ' + method + ' does not exist on jQuery.route'); } return result; }; }(window, jQuery)); </code></pre> <p>So I can use this as a router and set a route like so:</p> <pre><code> $(".element").add("route", "/foo/bar/&lt;path:params&gt;", 2).done(function(params){ // do something, for example console.log(this); console.log($(this)); console.log("which will be the same = $('.element')); }); </code></pre> <p>Hope it's more clear now.</p> <p>Thanks for having a look.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload