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 thoughts on “jQuery.strict = true”

Comments are closed.