Last active
January 2, 2019 10:42
-
-
Save brookjordan/13f14d341c3fdf72056031c311efc736 to your computer and use it in GitHub Desktop.
Allow an object property to be accessed as a method or… non method?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Build the asserter | |
let assert = { | |
get modal() { | |
let modal = selector => ({ | |
get exists() { | |
return assert.modalExists(selector); | |
}, | |
}); | |
Object.assign(modal, { | |
get exists() { | |
return assert.modalExists(); | |
}, | |
}); | |
return modal; | |
}, | |
modalExists(selector = '') { | |
return !!document.querySelector(`${selector}.modal`); | |
}, | |
}; | |
// Testing the asserter | |
console.log(assert.modal.exists); // => false | |
let modal = document.createElement('div'); | |
modal.className = 'modal'; | |
document.body.appendChild(modal); | |
console.log(assert.modal.exists); // => true | |
console.log(assert.modal('error').exists); // => false | |
modal.className = 'modal error'; | |
console.log(assert.modal('.error').exists); // => true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment