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

  • Home
  • SEARCH
  • 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 8843767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:18:53+00:00 2026-06-14T11:18:53+00:00

Apologies – I have no idea to how to describe this. Example: function OutputNumber(number)

  • 0

Apologies – I have no idea to how to describe this. Example:

function OutputNumber(number) {
    this.outputThisInstead = function (otherNumber) {
        console.log(otherNumber);
    }
    console.log(number);
}

Desired usage:


new OutputNumber(1);

Console output: 1


new OutputNumber(1).outputThisInstead(2);

Console output: 2


Naturally, 1 will always be written to the console, irrespective of what else is called on the object.

I’m after this particular syntax, as well as the behaviour – attaching a function onto the initialisation. It feels impossible since the object must be constructed before any function is called on it, but is this achievable any other way?

  • 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-06-14T11:18:55+00:00Added an answer on June 14, 2026 at 11:18 am

    It would be possible with a time delay (e.g., in a browser environment, setTimeout or similar) and a flag. Not desirable, but possible.

    Without that, no, you can’t base the action of the constructor on something that hasn’t happened yet. You’d have to instead pass something into the constructor to let it know what was going on.

    Browser example (again, I don’t recommend this):

    function OutputNumber(number) {
        var handle = 0;
        this.outputThisInstead = function (otherNumber) {
            if (handle) {
                clearTimeout(handle);
                handle = 0;
            }
            console.log(otherNumber);
        }
        handle = setTimeout(function() {
            console.log(number);
        }, 0);
    }
    

    From your comment on the question:

    This is the end of a sequence of chaining objects/functions, that I’m experimenting with. For example:

    Assert.that(1).is.not(2).because('output this message if fails');
    

    Here not(2) returns an object on which because can optionally be called. The behaviour of the object would depend on because being called.

    Rather than have the behavior of an earlier function in the chain depend on a later function in the chain, I’d probably add an .end() at the end of something:

    Assert.that(1).is.not(2).because('output this message if fails').end();
    

    end would output whatever message/messages was/were stored by the previous functions. No need for black magic. Obviously this suffers from the fact that people could fail to put the .end() on, but you need some kind of trigger that it’s okay to do the output, if you want the output to change based on an optional subsequent function call.

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

Sidebar

Related Questions

Apologies for no code. But I don't have a starting point for this question.
Apologies if this has been answered previously. Say I have the following table: colA
Apologies if this is a fairly simple question. I have a rich text area
Apologies if this is a newbie question, but I have been unable to resolve
Apologies in advance, because I suspect this may be a silly question. I have
apologies if this has been asked before but I have searched the site... Anyway,
Apologies if this is too abstract, but hopefully someone can help. I have a
Apologies if this is a duplicate, 20 minutes of searching didn't yield this exact
Apologies for the poor question title. I have two tables, jobs and Persons-Jobs. Jobs
Apologies if this doesn't make sense, i'm not much of an experienced programmer. Consider

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.