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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:01:48+00:00 2026-06-13T19:01:48+00:00

Looking for a construct in javascript which works like the destructor in stackbased or

  • 0

Looking for a construct in javascript which works like the destructor in stackbased or local object in c++, e.g.

#include <stdio.h>
class M {
public:
  int cnt;
  M()        {cnt=0;}
  void inc() {cnt++;}
  ~M()       {printf ("Count is %d\n", cnt);}
};
...
{M m;
 ...
 m.inc ();
 ...
 m.inc ();
} // here the destructor of m will printf "Count is 2");

so this means I am looking for a construct which does an action when its scope is ending (when it “goes out of scope”). It should be robust in the way that it does not need special action at end of scope, like that destructor in c++ does (used for wrapping mutex-alloc and release).

Cheers, mg

  • 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-13T19:01:49+00:00Added an answer on June 13, 2026 at 7:01 pm

    If the code in the scope is guaranteed to be synchronous, you can create a function that calls the destructor afterwards. It may not be as flexible and the syntax may not be as neat as in C++, though:

    var M = function() {
      console.log("created");
      this.c = 0;
    };
    
    M.prototype.inc = function() {
      console.log("inc");
      this.c++;
    };
    
    M.prototype.destruct = function() {
      console.log("destructed", this.c);
    };
    
    
    var enterScope = function(item, func) {
      func(item);
      item.destruct();
    };
    

    You could use it as follows:

    enterScope(new M, function(m) {
      m.inc();
      m.inc();
    });
    

    This will be logged:

    created
    inc
    inc
    destructed 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

sorry for my stupidity but I'm looking for a way to construct an object
I'm looking for a library that will allow me to construct a Tree and
I'm looking to reimplement some Tcl code that uses the socket -server construct [1].
I've been looking for a way to call a class's constructor that is analogous
I'm looking to construct a script that would go through an XML file. Would
I'm looking for a way to remove these characters from a string in javascript
I'm rewriting a JavaScript project, and I want to be able to use object
I need to deserialize some JavaScript object represented in JSON to an appropriate C#
I am looking for a Javascript solution to this problem: I have an XML
I've been looking up JavaScript patterns to get started on. I know enough JavaScript

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.