Created
November 7, 2020 22:35
-
-
Save yaakov123/769dc61448adb1ba7e8ec3a90cbeed07 to your computer and use it in GitHub Desktop.
This file contains 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
// 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