The item toggle before the list of children

I have a function that stores a list of div children to keep them in their original order as they move around

an object with children is created as follows:

$('#container').data( 'list', $('#container').children() );

When I add a child to this list, I can use push:

$('#container').data( 'list' ).push( element );

but when I try to click an item in front of this list, for example:

$('#container').data( 'list' ).unshift( element );

I would expect it to elementbe pressed before data('list'), but I get an error:

$ (...). data (...). unshift is not a function

? , , push , splice slice , , .

JSFiddle, .

+4
1

, , "... unshift ", , $('#container').data('list'). $('#container').data('list',$('#container').children());, , jQuery. , , "", ? :

var cnsl = $('#console');
function log(s) {
  cnsl.append('<span>'+s+'<span><br/>');
}

var children = $('#container').children();

var res = (children.length !== undefined) ? 'has' : 'doesn\'t have'
log('A jQuery object ' + res + ' a length');
res = (children[1] !== undefined) ? 'can' : 'can\'t';
log('A jQuery object ' + res + ' be addressed by index using square brackets');
res = (children.push !== undefined) ? 'has' : 'doesn\'t have'
log('A jQuery object ' + res + ' a push property');
res = (typeof children.push === 'function') ? 'is' : 'isn\'t'
log('A jQuery object\ "push" property ' + res + ' a function');
res = (children.push === Array.prototype.push) ? 'is' : 'isn\'t'
log('A jQuery object\ "push" property ' + res + ' the same function as Array.prototype.push');

res = (children instanceof Array) ? 'is' : '<b>isn\'t</b>'
log('A jQuery object ' + res + ' an instance of Array');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='container'>
  <div class='child'></div>
  <div class='child'></div>
  <div class='child'></div>
</div>

<div id="console"></div>
Hide result

, , , .

, $('x').push === Array.prototype.push?
, , , , , jQuery, . , - this.push(..) . slice() , .

jQuery().push() , , .


?
! !

$('#container').data( 'list', $('#container').children().get() );

//...
$('#container').data( 'list' ).push( element.get(0) );
$('#container').data( 'list' ).unshift( element.get(0) );

( )


;

jQuery . jQuery().unshift().

+3

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


All Articles