Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify Note 1 under addEventListener section within documentation #117

Open
Aaronius opened this issue Mar 23, 2021 · 0 comments
Open

Clarify Note 1 under addEventListener section within documentation #117

Aaronius opened this issue Mar 23, 2021 · 0 comments

Comments

@Aaronius
Copy link

Aaronius commented Mar 23, 2021

Thanks again for this data layer!

In the documentation, there's this note under the addEventListener section:

Note 1: you should pass a reference to a function as the listener argument, not an anonymous function. To remove the listener you just pass the same reference to the removeEventListener method. If you pass an anonymous function, you won't be able to unregister it: this behavior is similar to the unregistering of a DOM event listener (EventTarget.removeEventListener()).

I think there's a misunderstanding about what an anonymous function is. An anonymous function is just a function without a name, but an anonymous function can still be stored in a variable that can be referenced later. For example, this uses an anonymous function:

var adobeDataLayer = [];
var addToCartListener = function() {
  console.log("product added to cart");
};

adobeDataLayer.push(function(dl) {
  dl.addEventListener("addToCart", addToCartListener);
});

setTimeout(function() {
  adobeDataLayer.push(function(dl) {
    dl.removeEventListener("addToCart", addToCartListener);
  });
}, 3000);

I think what the documentation is intending to say is that the developer needs to store a reference to the function if they would like to remove the event listener later. Whether that's done using a named function or an anonymous function doesn't really matter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant