46

In the course of building my project, I discovered that it's possible to overload the addEventListener object - I've seen a few questions that asked the question about adding one eventListener to multiple objects, but the questions are over two years old, and really, I just want to put out there a simple HOWTO so others might find it...how do I do that? Is this the place for it?

(Still with the noob questions, I know...)

15
  • 18
    Now I'm really curious as to how exactly one overloads addEventListener ?
    – adeneo
    Commented Jul 22, 2014 at 15:14
  • 2
    @adeneo: ... or why anyone would/ would want to. p.s.: jsfiddle.net/kKXmL. Not that that would be remotely x-browser.
    – Matt
    Commented Jul 22, 2014 at 15:15
  • @adeneo: It's quite easy, actually: var old = Element.prototype.addEventListener; Element.prototype.addEventListener = function() { /*...*/ }; (example) and then do what you want to do. Should work on IE9 and up and anything modern. :-) (And on IE8, you could add it, IE8 does have Element.prototype.) Commented Jul 22, 2014 at 15:19
  • @Matt: One reason would be if you had a large codebase that assumed useCapture was optional and you need to run it on some older browser that required useCapture. :-) Not necessarily a good reason, but... Commented Jul 22, 2014 at 15:20
  • @T.J.Crowder - Yeah, was actually thinking about just overwriting it, or prototyping it to nodeList to make it work on a collection of elements etc. but there's no real magic there, and all it does is making the behaviour unpredictable
    – adeneo
    Commented Jul 22, 2014 at 15:21
  • @adeneo: Yeah, depends on what you're doing with it. Polyfilling the thing about useCapture being optional, or needing to track all calls to it, are just about the only use cases I can think of, both of which are better handled with a wrapper. Commented Jul 22, 2014 at 15:22
  • On the other hand, questions on why addEventListener doesn't work on a nodeList come up almost daily, so all the OP has to do is wait and pay attention, and then post the revolutionary code whenever one of those questions comes along.
    – adeneo
    Commented Jul 22, 2014 at 15:24
  • Watching this discussion and the recent reduction of my reputation score makes me think twice about posting anything. So maybe I'll ask it as a question on whether it's even a good idea or if I've actually done what I think I've done...
    – NovaDev
    Commented Jul 22, 2014 at 15:27
  • @NovaDev - That sounds like a reasonable idea. Post a question regarding your javascript code on SO, and ask for pros, cons etc. just don't make it to opinionated, but more technical about the code, and you'll probably get several good answers.
    – adeneo
    Commented Jul 22, 2014 at 15:30
  • Here's how I did it recently: stackoverflow.com/questions/24884239/… Try to create a canonical resource that everyone on the web can point to.
    – usr
    Commented Jul 22, 2014 at 22:56
  • I'll just leave this here: en.wikipedia.org/wiki/Jeopardy!
    – Marco13
    Commented Jul 23, 2014 at 8:41
  • Well, can't we actually "overload" almost anything in js this way? Prototype mechanics were set down in ecma-262 like 15 years ago. Commented Jul 24, 2014 at 20:31
  • Don't bother posting this, this is going to be closed as a dupe as it's such a common technique.
    – simonzack
    Commented Dec 9, 2014 at 16:31
  • see also: Is it OK to promote my own code by self-answering my own questions?
    – gnat
    Commented Dec 14, 2014 at 10:48
  • You mean the answer "42"? Commented Aug 12, 2020 at 15:47

4 Answers 4

119

Rather than creating a "How To" page, you can do this by creating your own question and posting the answer at the same time - as long as your question is not duplicating the existing questions on the subject.

If your question would be a duplicate then post your answer on one of existing questions - the one that's most relevant - and consider flagging the other questions as duplicates. Though you need to be careful here to make sure that the question are indeed duplicates. Don't post your answer to all the questions though. This will be flagged up and you will end up with the duplicate answers deleted which will seriously damage your ability to answer other questions in future.

1
  • 28
    I think it is important to remember that such questions need to be questions. So the solution is to write a good question which "happens" to have an answer of the one that was pre-discovered. (Note: I know you know this, I don't know if others do. I posted this comment because I have made this mistake, and this success)
    – Justin
    Commented Jul 22, 2014 at 22:48
81

You should still post your answer to a question that is 2 years old. You found it in your searching, someone else will as well. Leave it there to help others.

6
  • 1
    Answers should only be posted once. See Chris' answer.
    – halfer
    Commented Jul 22, 2014 at 23:57
  • 7
    krillgar - no I am pretty sure that is not what @halfer is insinuating. He is saying that OP should post his answer to only one of the questions - not the same answer to multiple questions.
    – Floris
    Commented Jul 23, 2014 at 1:19
  • I've edited my question to clarify leaving an answer for a single question. I had intended it in a general sense where any time that you come across this situation, you should leave your answer as opposed to leaving the same answer anywhere that it might fit.
    – krillgar
    Commented Jul 23, 2014 at 10:30
  • Ah, apologies if I didn't expand very well on my comment above. Yes, @Floris is right; it sounded like you were endorsing the copy+pasting of one answer across duplicates, and I would be inclined to discourage that. It is better to post once and flag others you believe are dups.
    – halfer
    Commented Jul 23, 2014 at 18:59
  • 2
    I don't think that there's anything wrong with (within reason) answering multiple pre-existing questions with essentially the same answer. (At least I see it done fairly often and have probably done it once or twice myself.) But it's probably a bit iffy to do that and create your own question.
    – Hot Licks
    Commented Jul 24, 2014 at 20:17
  • Answers must be at least somewhat tailored to the question. Posting the exact same answer repeatedly is not ok - meta.stackoverflow.com/questions/258040
    – nobody
    Commented Aug 24, 2014 at 20:54
19

Answer your own question - Share your knowledge, Q & A style:

Screenshot highlighting the "Answer your own question" checkbox

1
  • I wonder why noone had brought up that help center link before. You should put at above the image, however.
    – Bergi
    Commented Jul 24, 2014 at 21:17
4

If you first research for existing questions about the topic you are talking about then it is perfectly fine to ask and answer your own question.

This is what SO is all about, to help people to find relevant solutions to their problems.

Jeff Atwood wrote an article in the Stackoverflow blog about this specific topic:

So …

  • if you have a question that you already know the answer to
  • if you’d like to document it in public so others (including yourself) can find it later
  • it is OK to ask, and answer, your own question on a relevant Stack Exchange site.

To be crystal clear, it is not merely OK to ask and answer your own question, it is explicitly encouraged.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .