Object Key Order and Reflect.ownKeys
copy const obj = { 11 : 'eleven' , 23 : 'twenty-three' , 1 : 'one' , 2 : 'two' , '-1' : 'negative 1' } ; console.log ( Reflect.ownKeys ( obj) )
Try it out
I noticed that for in..
over an object was giving me weirdly consistent results across all browsers the other day and stumbled upon this.
Great news, but it’s Reflect.ownKeys
for me… now that IE11 is dying/dead.
selectAll Thoughts
copy const lookup = new WeakMap( ) let id = 0 ; export const els = selectAll( document.body ) export const parentOf = el => lookup.get ( el) export function selectAll( parent) { const els = { id, el: parent, all: { } } const allEls = [ ...parent .querySelectorAll ( '*' ) ] id++ for ( let i in allEls) { const el = allEls[ i] // any element can lookup `els` lookup.set ( el, els) const tag = el.tagName .toLowerCase ( ) if ( el.id ) { els[ el.id ] = el tag === 'template' && ( els[ el.id + 'Tmpl' ] = ( ) => { const instanceEl = el.content .cloneNode ( true ) .firstElementChild return selectAll( instanceEl ) } ) } if ( el.classList .length > 0 ) { const name = el.classList [ 0 ] if ( ! els[ name] ) { els[ name] = el els.all [ name] = [ el] } else { els.all [ name] .push ( el) } } } return els; }
Whenever I code a vanilla gui for some personal project/experiment I write some variation of this. I did this one a few days back to see how it would work with template
tags.
xPath contains (document.evaulate)
copy // hack to shove some html on the page since snippet.zone // quick editor does not yet support HTML :P document.body .innerHTML += ` < p> This is some text to search...</ p> < p> Here are some random words "Robot, Cat, Swan, Shadow" ...</ p> < button> Say Hello Robot</ button> `; /////////// // find elements with the word `search` let els = document.evaluate ( "//p[contains(., 'search')]" , document, null , XPathResult.ANY_TYPE , null ) ; let el; while ( el = els.iterateNext ( ) ) { console.log ( 'match "search"' , el) ; } els = document.evaluate ( "//body/*[contains(., 'Robot')]" , document, null , XPathResult.ANY_TYPE , null ) ; while ( el = els.iterateNext ( ) ) { console.log ( 'match "Robot"' , el) ; }
Try it out…
Now that IE is almost dead… we can use xPath – take a look at your console
xPath and svg
copy // hack to shove some html on the page since snippet.zone // quick editor does not yet suppor HTML :P document.body .innerHTML += ` < svg style= "overflow:visible" > < rect x= "10" y= "10" fill= "red" width= "30" height= "10" /> < rect x= "10" y= "20" fill= "blue" width= "30" height= "10" /> < rect x= "10" y= "30" fill= "purple" width= "30" height= "10" /> </ svg> `; const svgPrefix = pre => pre === 'svg' && 'http://www.w3.org/2000/svg' ; // https://stackoverflow.com/questions/31162358/extract-svg-element-by-using-document-evaluate let els = document.evaluate ( '//svg:svg/*[@fill="blue"]' , document, svgPrefix, XPathResult.ANY_TYPE , null ) ; const results = [ ] ; while ( ( el = els.iterateNext ( ) ) ) { // save results to avoid mutation error results.push ( el) ; } results[ 0 ] .x .baseVal .value = 50 ; results[ 0 ] .setAttribute ( 'stroke' , 'orange' ) ; results[ 0 ] .setAttribute ( 'stroke-width' , 2 ) ;
Try it out…
Now that IE 11 is almost dead… we can use document.evaluate
– I knew there would be some trick to get it to work on svg… Found the answer from user Martin Honnen on stackoverflow – very cool.
Of course this one can be done with document.querySelectorAll('*[fill="blue"]')
– but xPath can do some other stuff that css selectors can’t exactly do yet…
super Methods Javascript
copy class Person { constructor( name, email) { this .name = name; } toString( ) { return `name: ${ this .name } `; } } class Teacher extends Person { constructor( name, subject) { super ( name) ; this .subject = subject; } toString( ) { return super .toString ( ) + ` subject: ${ this .subject } `; } } const teacher = new Teacher( 'testname' , 'testSubject' ) ; console.log ( teacher.toString ( ) ) ;
Try it out…
This is from an old stackoveflow answer of mine …