My code works, but I was taught to bind 'this' to the event listener function. In my web component class constructor, I have a button in the shadow root. In the constructor I also added the click event listener on the button and went ahead and defined the on click function as an arrow function, ie this.clickHandler = (e) => {...}. However, I did not bind 'this' to the event listener function, I just used this.button.addEventListener('click', this.clickHandler). The code seems to work fine, and, like many others before me I'm sure, I'm left needing a bit of guidance about binding 'this'. Many thanks.
Javascript / ES7: do I need to bind 'this' in my particular Web Component class constructor?
118 Views Asked by ahpto At
1
There are 1 best solutions below
Related Questions in JAVASCRIPT
- Using Puppeteer to scrape a public API only when the data changes
- inline SVG text (js)
- An array of images and a for loop display the buttons. How to assign each button to open its own block by name?
- Storing the preferred font-size in localStorage
- Simple movie API request not showing up in the console log
- Authenticate Flask rest API
- Deploying sveltekit app with gunjs on vercel throws cannot find module './lib/text-encoding'
- How to request administrator rights?
- mp4 embedded videos within github pages website not loading
- Scrimba tutorial was working, suddenly stopped even trying the default
- In Datatables, start value resets to 0, when column sorting
- How do I link two models in mongoose?
- parameter values only being sent to certain columns in google sheet?
- Run main several times of wasm in browser
- Variable inside a Variable, not updating
Related Questions in WEB-COMPONENT
- how can i Integrate React Web Component with Angular and Passing Props
- JS CustomElements - Self 'registration' upon first instantiation
- How can I make jest support importing HTML file inside TS files?
- How to style a custom HTML element based on child node selector(s), using a shadow DOM attached stylesheet?
- CustomElementRegistry repeat registration error
- Styling swiper web component navigation buttons
- Why cant I select all selected option in IComboBox from @fluentui/react
- Getting Cannot read property 'polyfillWrapFlushCallback' of undefined
- How to render a Lit element component in a Nuxt 3 project?
- Can you have an `<li>` in a autonomous custom element with the parent `<ul>` not in the same ShadowDOM?
- How to manage routes and templates in a website?
- Why always Web component(Custom component)'s shadow-root copy own root's style
- Can I selectively stop a ::part pseudo selector from overriding shadow dom css
- Why isn't my custom element being garbage-collected?
- Can Vite serve up-to-date compiled source at a URL?
Related Questions in ECMASCRIPT-2016
- Onclick is not working over the element in React
- sort array of objects by order of the property in a fixed format
- how do i export mysql createPool( in node.js using ES7 format
- Javascript - Remove items that are duplicates from array and return an array with duplicates and other one with all other items
- check attribute with value 0 exists or not returns false though it should return true as it exists
- How to replace value in javascript or ecmascript?
- Error: listen EADDRNOTAVAIL: address not available 127.0.0.0:5173
- How does a JavaScript developer find implementing classes of an interface?
- Get matching items by comparing two arrays
- How to make vitejs compile class private fields to legacy code?
- How does the setTimeout() in javascript knows how many times to loop inside of a for loop?
- How to use await with setTimeout
- How do I do "Reverse destructuring"
- how ES7 import module's object in Node.js
- How to map an array base on other arrays without nested looping - JS/TS, ES6(7)?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Create test code to see all options:
The
onclickEvent Handler Property creates better readable code, IMHO;but you can only assign one per element
Can create multiple
clickhandlers on one element, and pass arguments.is for when you never want to access scope and parameters inside the class method
IMHO, Do not use it! Because junior team members don't understand why behaviour is different from normal class methods.
(again IMHO) is oldskool notation, when you see it in code
Note: you can pass arguments with
bind. But it will prepend them to the bound method;That means in
clicked(evt)theevtparameter now becomes your first argument... more confusion for juniors.Very deep dive on 'this': How does the "this" keyword work, and when should it be used?