adobe / adobe/adobe-client-data-layer

Clarify Note 1 under addEventListener section within documentation

Open
#117 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
122
Forks
28
PR merge metrics
No merged PRs in 30d

Description

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:

```js
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.