Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4041716
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:54:55+00:00 2026-05-20T12:54:55+00:00

I’m trying to write an object relationship that follows an observer pattern where the

  • 0

I’m trying to write an object relationship that follows an observer pattern where the observer cares about a specific set of events that occur on the subject.

I’m not sure if this is 100% standard, but the way I’ve constructed it, these event objects are defined within the observer, with custom callbacks that will fire when the event occurs. When an event occurs on the subject, it goes through all it’s observers and looks to see who is watching for this event. If it finds observers watching for this event, it triggers the observer event’s callback.

Since I want my observers to have the flexibility to add and remove, on the fly, events that care to watch, I need for the event object to have the ability to delete itself after running it’s callback… say, for instance, if my observer only wants to respond to an event once, then no longer monitor it.

This seems like a good plan, but I know that a JavaScript object can’t have call delete() on itself.

I was just curious is anyone else out there had ran into this and had come up with an effective solution.

My only thought was that I could pass the reference to the parent observer object, to it’s child event, then when the callback happens, I could call a method within the parent… something like removeEvent(this) passing the event itself to this function. The removeEvent function could then splice out the event from it’s array of events. The only complicated problem would be finding the location of this event object in the array. (taking suggestions on this too, thanks!).

Thanks in advance for the help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:54:56+00:00Added an answer on May 20, 2026 at 12:54 pm

    I thought I’d have a go at answering your question but I’m not 100% sure how you’ve implemented your observer pattern. Perhaps there’s something useful in my snippet below. I’ve assumed that the observation is done via callback functions and I’ve assumed nothing about the format of that data so I’m using strings.
    The crux of my solution is that the callback receives a reference to the Subject (which is usual in the observer pattern). This reference can be used to detach the callback from the Subject.

    var Subject = {
        observers: {},
    
        attach: function( eventType, fn ) {
            if( !this.observers[eventType] ) this.observers[eventType] = [];
            this.observers[eventType].push( fn );
        },
    
        detach: function( fn ) {
            var newObservers,
                eventType,
                i;
            for( eventType in this.observers ) {
                newObservers = [];
                for( i = 0; i < this.observers[eventType].length; i++ ) {
                    if( this.observers[eventType][i] !== fn ) newObservers.push( this.observers[eventType][i] );
                }
                this.observers[eventType] = newObservers;
            }
        },
    
        notify: function( eventType, data ) {
            var i, observers = this.observers[eventType].slice(0);
            for( i = 0; i < observers.length; i++ ) {
                observers[i]( data, this );
            }
        },
    
        poke: function() {
            this.notify( 'testing', 'I got poked' );
        }
    
    };
    
    var Observer = {
    
        logEvent: function( data, subject ) {
            console.log( 'Every time: ' + data );
        },
    
        logEventOnce: function( data, subject ) {
            console.log( 'Just once: ' + data );
            /*
             * THE CRUX
             */
            subject.detach( arguments.callee );
        }
    };
    
    Subject.attach( 'testing', Observer.logEvent );
    Subject.attach( 'testing', Observer.logEventOnce );
    
    Subject.poke();
    //Every time: I got poked
    //Just once: I got poked
    
    Subject.poke();
    //Every time: I got poked
    
    Subject.poke();
    //Every time: I got poked
    
    Subject.poke();
    //Every time: I got poked
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.