javascript - what is prevObject and context in pushstack in jquery ? -


i understand how pushstack function works , understand how use plugins (i guess that's used , internal use , end() function , other similar methods) .

now below jquery source of pushstack , have :

pushstack: function( elems ) {     // build new jquery matched element set     var ret = jquery.merge( this.constructor(), elems );     // add old object onto stack (as reference)     ret.prevobject = this;     ret.context = this.context;     // return newly-formed element set     return ret; }, 

theres lot going on in function , kind of part of , have small problem , understanding below lines of code :-

ret.prevobject = this; ret.context = this.context; 

what prevobject , context ? can give me clue , does't seem javascript thing ?

basically prevobject used allow flexibility jquery selectors chaining syntax.

most of jquery's dom traversal methods operate on jquery object instance , produce new one, matching different set of dom elements. when happens, if new set of elements pushed onto stack maintained inside object. each successive filtering method pushes new element set onto stack

everytime make new filter new jquery instance constructed matching selector , previous 1 stored in allow .end() , addback() functions work (most of dom transversal functions use pushstack internally). if property not used every selector forget previous 1 , not behave stack. think parent instead of prevobject , make easier understand.

the context property deprecated in jquery 1.10 , used supporting live() method must added because returning object constructed merging empty jquery constructor first parameter , set of elements second.

var ret = jquery.merge( this.constructor(), elems ); 

because .merge returns first array modified it's context property might not have correct value overwritten correct 1 this.context

check this jsfiddle , open console. see first value of prevobject document making new selector , ul element since filter using find. can go last filter , lookup whole chain of selectors document again.

i recommend use jquery api instead of property reference in production allow example know result of selectors have been applied obtain given set of dom elements.


Comments

Post a Comment

Popular posts from this blog

c# - Where does the .ToList() go in LINQ query result -

Listeners to visualise results of load test in JMeter -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -