Skip to content

Instantly share code, notes, and snippets.

@yaakov123
Created November 7, 2020 22:35
Show Gist options
  • Save yaakov123/769dc61448adb1ba7e8ec3a90cbeed07 to your computer and use it in GitHub Desktop.
Save yaakov123/769dc61448adb1ba7e8ec3a90cbeed07 to your computer and use it in GitHub Desktop.
// Store a reference to the native appendChild function
const nativeAppend = Node.prototype.appendChild;
// Override the appendChild method
Node.prototype.appendChild = function(node) {
// If the node is a "robber" don't append it
if (node.id === 'robber') {
console.log('denying robber entry to the bank');
return;
}
console.log('letting person into the bank');
// use the native append function to add
// the node to the DOM
return nativeAppend.call(this, node);
}
// Get the "bank" from the DOM
const bank = document.body.querySelector('#bank');
const accountOwner = document.createElement('div');
accountOwner.id = 'owner';
// Let the "account owner" into the bank (append it to the DOM)
bank.appendChild(accountOwner); // logs "letting person into the bank" to console
const robber = document.createElement('div');
robber.id = 'robber';
// Prevent the "robber" from accessing the bank (don't append to DOM)
bank.appendChild(robber); // logs "denying robber entry to the bank" to console
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment