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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:03:39+00:00 2026-06-04T17:03:39+00:00

Say I have a pure constructor function (containing nothing but this.Bar = bar) 1)

  • 0

Say I have a pure constructor function (containing nothing but this.Bar = bar)

1) When I call it from another function, can I pass the caller function’s arguments directly when I call or must I do var myBar=new bar, myBar.Bar=thebar, where the bar is a caller argument?

2) Will the constructor still instantiate even if it doesn’t get all the args?

3) How can I check if one of the args is unique, IE no other instance of the object has this value for the property in question? Specifically, I want to assign each object a unique index at creation. Maybe array?

Many thanks in advance

  • 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-04T17:03:41+00:00Added an answer on June 4, 2026 at 5:03 pm

    Say I have a pure constructor function (containing nothing but this.Bar = bar)

    I’m going to assume you mean:

    function MyConstructor(bar) {
        this.Bar = bar;
    }
    

    (Note: The overwhelming convention in JavaScript is that property names start with a lower-case letter. So this.bar, not this.Bar. Initially-capped identifiers are usually reserved for constructor functions.)

    1) When I call it from another function, can I pass the caller function’s arguments directly when I call or must I do var myBar=new bar, myBar.Bar=thebar, where the bar is a caller argument?

    You can pass them directly:

    function foo(a, b, c) {
        var obj = new MyConstructor(b);
    }
    

    2) Will the constructor still instantiate even if it doesn’t get all the args?

    The number of arguments passed is not checked by the JavaScript engine. Any formal arguments that you don’t pass will have the value undefined when the function is called:

    function MyConstructor(bar) {
        console.log(bar);
    }
    var obj = new MyConstructor(); // logs "undefined"
    

    3) How can I check if one of the args is unique, IE no other instance of the object has this value for the property in question? Specifically, I want to assign each object a unique index at creation. Maybe array?

    In general, that’s usually not in-scope for a constructor. But yes, you could use an array, or an object, to do that.

    var knownBars = [];
    function MyConstructor(bar) {
        if (knownBars.indexOf(bar) !== -1) {
            // This bar is known
        }
        else {
            // Remember this bar
            knownBars.push(bar);
        }
    }
    

    Of course, indexOf may not be what you want for searching, so you may need to use some other method of Array.prototype or your own loop.

    Another way would be to use an object; this assumes that bar is a string or something that can usefully be turned into a string:

    var knownBars = {};
    function MyConstructor(bar) {
        if (knownBars.indexOf(bar) !== -1) {
            // This bar is known
        }
        else {
            // Remember this bar
            knownBars[bar] = 1;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is pure curiosity. Let's say I have a resource Users and want to
This is a little contrived, but say I have a class interface like this:
This question is for pure curiosity. Lets say I have lighttpd installed on my
Lets say have this immutable record type: public class Record { public Record(int x,
say i have a nvarchar field in my database that looks like this 1,
Say we have a class inheriting from two base classes (multiple inheritance). Base class
Lets say you have an normal song with two layers, one instrumental and another
Say we have an abstract base class IBase with pure virtual methods (an interface).
Say I have a abstract base class and I want to have a pure
I have a question regarding EC2. Let's say I have a pure EC2 instance

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.