jQuery.strict = true

(originaly published 2009 Mar 20)
“strict” mode? Why? To make jQuery enabled web applications, faster and behave better, I suggest introduction of jQuery.strict. The purpose is to be able to impose strict logic of jQuery usage.

Exactly the same intent as ECMA5 “use strict” directive


For example on jQuery forum, someone has asked How best to compare two jQuery objects?. A question which reveals a deep confusion. Mainly because fundamental OO concepts and thinking have NOT been applied.
In oo patterns parlance, jQuery is an implementation of the visitor pattern. And as its state it contains pointers to the elements of the hierarchical structure it operates on. References to dom elements. Why comparing two visitors? When are they equal? Why do we have two (or more) visitors at all? Even if one has two jQuery instances operating on the same set of elements , or even on different sets which are overlapping, then what happens if one jQuery instance removes the element from the dom tree? What happens is that tThe other could happily proceed to work on the removed element. Not good.

Ok, back to jQuery.strict. If I (we?) introduce this concept and variable we could impose some logic and develop some solutions to actually speed up sites based on jQuery.

For example we can imagine this already discussed, equality extension, being developed. To make this whole implementation less problematic, we can have embedded a less painful (read: faster) solution, with the proper logic imposed. If jQuery.strict == true. And vice versa, if one wants to have a slower code. Consider this:

Another usage of jQuery.strict , which immediately springs to mind, is with the context enforcer plugin, which if used can dramatically speed up pages using jQuery.

UPDATE 2009-03-26: after comment 1, my own re-think and discussion on JQ dev forum, I
have decide to introduce $scope() intead of replacing $(). This is much less intrusive but still
can be enforced and has a name which describes the intent 100%

For the purpose of this example, we might Imagine that IE has no issues with getElementById, so we do not instantiate the whole jQuery just to select element by its ID. In reality we would need to use jQuery(“#id”)[0], which will make things slower.

In any case, I know companies and team managers, which would very gladly have jQuery.strict = true, always. And. Enforce ubiquotus  $scope() usage.

Update: 2009 Aug
Brandon ( jQuery core team) has posted this article :
http://brandonaaron.net/blog/2009/06/24/understanding-the-context-in-jquery
After which in essence I feel (sort-of-a) vindicated. Brandon in this article prescribes the “correct” usage of the context.
And it is very simple: just use the DOM node as the context. Not selctor string, not jQuery instance, just use the plain old DOM node !
My comment: They should have told us earlier. But at last we are out of the woods on this one.
Thus, my jQuery “context enforcer” now becomes much simpler, and more effective of course.

Voila!

3 Replies to “jQuery.strict = true”

  1. I think generally this is a good idea but you may want to use some class selectors too, for which the context can be irrelevant. For example making rounded corners:

    or something like that.

    An other technical issue is using the DOMready callback:

    and as you noted

    To get around that it’d be better to replace the jQuery.fn.init function then restore the jQuery.prototype reference:

    Overall, I think this strictness can certainly make people use more reasonable selectors, and it’s enough to use this only in the development version, the public build will work without it as well — it will be just faster!

  2. Thanks Balazs,
    please see the amendments above. Now one just has to write:

    To have fast web app. Definitely faster than:

    And faster than :

    Especially if there is a lot of container’s.

  3. After Aug 2009, and Brandon Aaron’s article, http://brandonaaron.net/blog/2009/06/24/understanding-the-context-in-jquery
    we now know it is only meaningful to pass a dom node as a context argument, to $(selector,context).
    So instead of :

    One would write:

    Let me repeat: By jQuery team members “admition”, this is the only meaningful usage of the context argument.

Comments are closed.