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 6717843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:53:59+00:00 2026-05-26T08:53:59+00:00

I recently started programming JavaScript and thought everything would be good… Well today I

  • 0

I recently started programming JavaScript and thought everything would be good…
Well today I faced a problem I can’t solve on my own.
My tutorial/ learning project has a class called model. In this class there are several private and one public variable. This variable is of type CustomEvent:

      function Model(){
        /**
         * Array in which the questions are stored
         */
        var questions=new Array();
        var db;
        var valuesSplit="*";
        var tableName="quests";
        this.myEvent=new CustomEvent("my event");

So as you can see “myEvent” is public and can be called from outside. In this case it is an event which can be subscribed (this is done outside this class by other objects that want to listen) and it can be fired (this is done in the same class). And this is my problem.
How can I access myEvent within the model class?

I tried:

    this.myEvent.fire()

and:

    myEvent.fire()

But I always get “myEvent is not defined”.

  • 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-26T08:53:59+00:00Added an answer on May 26, 2026 at 8:53 am

    Probably the first thing to say is: JavaScript doesn’t have classes. The sooner you stop thinking of “JavaScript classes,” the better off you’ll be. 🙂 JavaScript has OOP, but not the kind with classes. Your Model function is called a constructor function.

    You can access myEvent from any code that has a reference to the object created by new Model, which includes code in your constructor (via this — e.g., the way you’re setting it up) and any function called with this referring to that object (or, of course, “externally” via someObjReference.myEvent).

    So probably this.myEvent.fire() is what you want, but the code you’re calling it from doesn’t have the right this value. That’s because in JavaScript, this is controlled entirely by how a function is called, not where the function is defined as it is in some other languages. See my blog articles Mythical methods and You must remember this for more details, but I’ve done a somewhat truncated discussion below.

    Here’s an example of a fairly standard way to set up a constructor function with useful methods that all instances share:

    function Foo() {
        this.myEvent = new CustomEvent("my event");
    }
    Foo.prototype.bar = function() {
        this.myEvent.fire();
    };
    
    // Usage:
    var f = new Foo();
    f.bar();          // Fires the event indirectly, via the code in `bar`
    f.myEvent.fire(); // Fires it directly
    

    Note that that only works if bar is called with this referring to an object with a myEvent property. It’s easy to call bar with this set to something else entirely:

    document.getElementById("someID").onclick = f.bar;
    

    When the click occurs, the bar function gets called, but this does not refer to an object created via Model. (It will refer to the element with the id “someID” instead.) And so this line in bar

    this.myEvent.fire();
    

    …will fail.

    If you’re used to class-based languages, you can see how this is totally different from, say, Java, C#, or C++. In those langauges, this inside bar will always refer to an object created via new Model. Not so JavaScript, which is both awkward and powerful.

    This flexibility of functions (not being bound to any particular object) is one of the biggest things to get used to, and take advantage of, in JavaScript. The other is how functions are closures, which is powerful but not complicated.

    So if this is set by how a function is called, how do you do that? There are two ways:

    1. Call the function by referencing it from an object property in the same expression as the call. That’s an awkward way of saying do this:

      var f = new Foo();
      f.bar();    // <== The key bit
      f["bar"](); // <== Also works
      

      The expression f.bar() does two things, which interact: The first thing it does is retrieve the property bar of the object referenced by f, and get that property’s value (which is a function reference). Then it calls that function (because of the ()). The way JavaScript works, because you did those two things in the same overall expression, the interpreter sets this to f during the call to bar for you. But note this key distinction:

      var f = new Foo();
      var b = f.bar;
      b(); // <== Different!
      

      Now when the bar function gets called, this will not be set to f (it’ll be set to the global object, which is window on browsers), because we’ve separated the property retrieval from the function call.

    2. Alternately, you can use the built-in features of JavaScript function objects, their call and apply functions. call and apply do exactly the same thing, the only difference between them is how you supply the arguments for the function. Example:

      var f = new Foo();
      f.bar(1, 2):       // <== Calls `bar` with `this` === `f` and passing in
                         //     the arguments 1 and 2
      var b = f.bar;
      b.call(f, 1, 2);   // <== Does the same thing as f.bar(1, 2)
      var args = [1, 2];
      b.apply(f, args);  // <== Does the same thing as f.bar(1, 2)
      

      E.g., call and apply allow you to set what this should be explicitly when you call the function. The only difference between them is that call accepts the arguments to give the function as further arguments to call, and apply accepts them as an array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently started programming in WPF and bumped into the following problem. I
I recently started programming my first Cocoa app. I have ran into a problem
I have recently started programming in PHP. I am building a cart in PHP.
I recently started diving into low level OS programming. I am (very slowly) currently
I've recently started down the road of programming in Objective-C, and I'm now looking
I have only recently started programming significantly, and being completely self-taught, I unfortunately don't
Possible Duplicate: Useful Regular Expression Tutorial Hello, I recently started coding in javascript. I
I recently started programming in WinForms using C#. I have a requirement where I
I recently started programming for Android using the Eclipse ADT. What I am frequently
I have recently started programming for the iOS Platform but now I need some

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.