Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Create and dispatch events javascript. Code execution simple delegation

Comming from a c# background, I just want to create an event in a certain point of my code, soas to be dispatched elsewere, meaning that if in some part of the code there has been a subscription, this delegate function is called.

So I tried to do:

function myFunction() {
    console.log("delegated call achieved!");
}

const myEvent = new Event('onMyConditionIsMet', myFunction, false);  

//at this point the program the subscription takes place
function whatever1() { 
    //...not meaningfull code
    myEvent.addEventListener('onMyConditionIsMet');
    //myEvent += myFunction; c# way subscription in case it makes sense 
}  

//at this point in the program, event subscription is checked and  
//delegate func run in case there has been a subscription
function whatever2() { 
    //...not meaningfull code
    myEvent?.invoke(); // ?.invoke(); would be the c# way to do it. 
}

All the examples I found are related to DOM events, but my case would be for events I create myself, think these are called synthetic events. Another assumption I make in this question is that there would be no arguments in the delegate call function, so, just to be clear with the naming, it would be a delegate with no arguments. Just pointing this because in c# events are just delegate funcs with no arguments, so a specific type of delegate. Not sure if this works the same way in Javscript.

What would be the approach to do this? (Meaning creating a simple event instance, subscribing, and executing the delegated code if there is any subscription)?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think the functionality you are looking for can be best obtained by using OOP/Classes.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#prototype_methods

Edit: see this also - "event" is deprecated, what should be used instead?

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!