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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:20:24+00:00 2026-06-07T20:20:24+00:00

I have function-constructor. I want to control what function (object) can call it. Here’e

  • 0

I have function-constructor. I want to control what function (object) can call it. Here’e the example:

function Bar() {

    // foo can be created only here, when Bar is instantiated
    var foo = new Foo();
}

function Foo() {

   // I'd like to have something like this here:
   if (caller != Bar) {
       alert("Not allowed, the caller is not Bar");
       return;
   }
}

var bar = new Bar();    // this is correct, Foo can be created inside Bar
var foo = new Foo();    // prints "Not allowed, the caller is not Bar" and exits

Is it possible to implement in JS? Are there some functions for such kind of control?

What will be created from Foo if the creation will be aborted this 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-07T20:20:25+00:00Added an answer on June 7, 2026 at 8:20 pm

    You can’t reliably identify the caller in a constructor across browsers, particularly in the new strict mode.

    Instead, you can define Foo() inside of Bar() or define them both inside of the same self executing function so that Foo() is not known outside the scope of Bar() and can thus only be created there.

    Some examples:

    // Bar() is only known within a single scope
    var Foo;
    (function(){
        Foo = function() {
        }
    
        function Bar() {
        }
    })();
    
    
    // Bar() is only known inside of Foo()'s constructor
    function Foo() {
        function Bar() {
        }
    }
    

    You may find this article instructional which discusses various ways of making instance data truly private: http://www.crockford.com/javascript/private.html. It’s not exactly the same as what you’re asking here, but uses some of the same techniques (hiding private data in a closure).

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

Sidebar

Related Questions

how can i call super constructor from the inheriting object? for example, i have
I have the below prog Object.prototype.inherit = function(baseConstructor) { this.prototype = (baseConstructor.prototype); this.prototype.constructor =
I have function getCartItems in cart.js and I want to call that function in
Say I have an object called FieldEdit . I define the function constructor for
Background : I have created this UserControl. In the constructor of the user control
when I have function I want to use as a constructor, say: function clog(x){
I want to make a constructor function that creates a documentElement object. As an
I have created classes dojo.declare(UNIT,null,{ _id:'', constructor:function(i){ this._id=i; }); and dojo.declare(ELEMENT, null, { _id:'',
Haskell beginner here. I want to pass a type parameter with JSON, and have
I have a function in .CS file. i want to return many values fetched

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.