Why does ExtJS want to check if the browser supports sorting?

I'm new to javascript, here is the code from ExtJS that bothers me:

supportsSort = (function() { var a = [1,2,3,4,5].sort(function(){ return 0; }); return a[0] === 1 && a[1] === 2 && a[2] === 3 && a[3] === 4 && a[4] === 5; }()), 

Can anyone tell me why ExtJS wants to run this test?

It’s better to attach some sample code.

+6
source share
2 answers

Hesitant will post this as an answer, since I admittedly just take an educated guess, but according to MDN , browser compatibility for Array.sort is listed as ECMAScript5 and β€œyes” for everything (as opposed to listing the actual version numbers) - leaving the test to actual support more or less redundant.

The variable name is probably a bit incompatible, because if you really follow what it does, the function that is passed to sort simply returns 0 ; usually you can return 1 or -1 depending on the conditions of comparison to manipulate the order of the array. Thus, the expected result is that the order of the array remains unchanged.

The return statement is just a chain of logical checks on whether the array is in the same order in which it was originally. Perhaps then this supportsSort flag should check if the browser / Javascript sorting function implementation is really a stable algorithm .

+2
source

I think this test is necessary to check whether the browser supports this function or not.

Since this is discussed in this post or in this link , you can find Array.prototype.sort([comparator]) . It is shown that not all browser versions support the sorting function.

Nowadays, there is no browser that does not support this feature (I'm talking about versions of latvia). But in case Ext Js is used for development for earlier versions, it becomes necessary.

0
source

Source: https://habr.com/ru/post/953830/


All Articles