in Education by
For pure educational and curiosity purposes, I am trying to create an element-wrapper object that allows me to tack-on my own properties and methods to an element. The behavior I'm trying to simulate is basically this: // get a button element to wrap const button = document.querySelector('button'); // some function that wraps new properties/methods around a given element function wrap(element) { this.customName = 'John'; this.customAge = 100; this.printName = function() { console.log(this.customName); } // ... // ...somehow inherit element fields... // ... } // wrap the button element const customElement = new wrap(button); // custom behavior: console.log(customElement.customAge) // output => 100 customElement.printName() // output => 'John' // legacy behavior console.log(customElement.clientHeight) // output => client height customElement.remove() // => should still call 'remove' on the element So, here I should be able to add my own methods/properties but still access the original fields normally. Is this even possible? I'm using a constructor function here as an example just to demonstrate the intended behavior, but I don't actually know if this would be relevant for the solution. I'm new to Javascript and I have done a ton of research on prototypes and classes, but I'm still confused on what approach I would take here. Edit: As Brad pointed out in the comments, I also tried this implementation using classes: class MyButton extends HTMLButtonElement { constructor() { super(); this.customName = 'John'; this.customAge = 100; } printName() { console.log(this.customName); } } const myBtn = new MyButton(); But this resulted in the error: Uncaught TypeError: Illegal constructor JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I haven't test this, but maybe something like this: // get a button element to wrap const button = document.querySelector('button'); // some function that wraps new properties/methods around a given element function wrap(element) { Object.defineProperties(element, { customName: {value:"John"}, customAge: {value:100}, printName:{value: () => console.log(element.customName)} }) return element } // wrap the button element const customElement = wrap(button); // custom behavior: console.log(customElement.customAge) // output => 100 customElement.printName() // output => 'John' // legacy behavior console.log(customElement.clientHeight) // output => client height customElement.remove() // => should still call 'remove' on the element Run code snippetExpand snippet

Related questions

0 votes
    how to push an element into an array in javascript?...
asked Dec 10, 2020 in Technology by JackTerrance
0 votes
    Which is the correct way to create an array in JavaScript? I) var myProg = []; II) var myArray = ["C","Java","C++","Python"]; III) ... 1. I, III 2. II, III 3. I, II 4. I, II & III...
asked Feb 24, 2021 in Technology by JackTerrance
0 votes
    I Know I can do this with static HTML, but I want to create dynamically, and I am struggling a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    I Know I can do this with static HTML, but I want to create dynamically, and I am struggling a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    He could not sleep because (i) there was an animal outside (ii) he thought savages had com (iii) his bed was uncoinfortable Select the correct answer from above options...
asked Dec 10, 2021 in Education by JackTerrance
0 votes
    How do we create and preload an image object in JavaScript? (a) Use new keyword (b) Call Image() ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    I'm trying to embed python into a C application. For the moment, I am trying to get the following ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I'm working on practicing my algorithms and getting into some bitwise stuff which I'm not too proficient ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I am using a program that talks to my COMM port, but I have made another program that I want to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I am using a program that talks to my COMM port, but I have made another program that I want to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I am using a program that talks to my COMM port, but I have made another program that I want to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
+1 vote
    Which is the correct way to create an Empty array in JavaScript? I) var myProg = []; II) var myArray = ["C","Java","C++","Python ... = new Array(); I, III II, III I, II I, II & III...
asked Oct 9, 2020 in Technology by JackTerrance
0 votes
    I am facing a problem that makes me to waste a lot of time. Each time I insert an element on a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I am facing a problem that makes me to waste a lot of time. Each time I insert an element on a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 10, 2022 in Education by JackTerrance
...