Skip to content

Instantly share code, notes, and snippets.

@brookjordan
Last active January 2, 2019 10:42
Show Gist options
  • Save brookjordan/13f14d341c3fdf72056031c311efc736 to your computer and use it in GitHub Desktop.
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?
// 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