Is this appointment right?

In some js library, I found this piece of code:

var start, end, sel, scrollPos, subst;

[start, end, scrollPos, sel] = getSelection();

Imo is an invalid destination, but the code works. You can help?

EDIT:

longer version:

...
var start, end, sel, scrollPos, subst;
        if (typeof(document["selection"]) != "undefined") {
            sel = document.selection.createRange().text;
        } else if (typeof(textarea["setSelectionRange"]) != "undefined") {
            [start, end, scrollPos, sel] = getSelection();
        }
...
+4
source share
1 answer

This seems to be a JS 1.7 feature called Destination Destructuring.

The purpose of destructuring allows you to extract data from arrays or objects using syntax that reflects the design of the array and object literals.

Literal expressions of objects and arrays provide an easy way to create custom data packets. By creating these data packets, you can use them the way you want. You can even return them from functions.

, , - , , , .

, , Perl Python.

... :

, , :

var a = 1;
var b = 3;
[a, b] = [b, a];

, Mozilla Firefox, , , . , , .

+5

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


All Articles