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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:19:37+00:00 2026-06-14T20:19:37+00:00

I am having a little trouble with my code. I understand the concept of

  • 0

I am having a little trouble with my code. I understand the concept of method chaining and constructor chaining but can’t get it to work right. Here is what I have so far:

function Set() {
    this.values = [];
}

Set.prototype.add = function() {
    for (i = 0; i < arguments.length; i++) {
        this.values.push(arguments[i]);
    }
}

var regSet = new Set();
regSet.add(10, 11, null);
console.log(regSet.values);  // → [10, 11, null];

function NonNullSet() {
    Set.apply(this, arguments);
}

NonNullSet.prototype = Object.create(Set.prototype);
NonNullSet.prototype.constructor = NonNullSet;

NonNullSet.prototype.add = function () {
    for (var i = 0; i < arguments.length; i++) {
        if (arguments[i] == null || arguments[i] == undefined) {
            throw new Error("Can't add null or undefined");
        }
    }

    return Set.prototype.add.apply(this, arguments);
}

var nonNull = new NonNullSet();
nonNull.add(10, 12, null);
console.log(nonNull.values);  // → undefined

As you can see from the code above, nonNullSet is a subclass of Set and I am trying to augment the add method by checking for null or undefined values. If they exist, just continuing looping. If they are valid values, call the add method of the Set super class, rather than rewriting it.

To me, this looks right and I am not getting the results I want, so something isn’t right. What am I doing wrong here?

  • 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-14T20:19:38+00:00Added an answer on June 14, 2026 at 8:19 pm

    I see two problems:

    • continue skips to the next loop iteration, but the loop doesn’t do anything anyway, so continue-ing inside it doesn’t change anything. Instead, you should actually call Set.add inside your loop, giving it one element at a time — but only if the element is non-null.
    • console.log(nonNullSet.values) should be console.log(nonNull.values): nonNullSet is your constructor, nonNull is your instance.

    Also, not exactly problems, but other things you should improve:

    • You should capitalize NonNullSet.
    • Inside Set.prototype.add, you should make i a local variable, by using the var keyword.
    • Your variable-names contain null, but instead of checking for nullity, you check for falsiness. (null is falsy, of course, but so are some other things.) I think this is a bit confusing.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have posted my code below, but essentially I'm having a little trouble. I
I'm having a little trouble trying to get WarpPointer to work out appropriately for
I'm having a little trouble with making the following piece of code work. I
I'm having a little trouble swapping rootViewControllers with animation. Here's the code that I'm
Im having a little trouble with a php script ive made. you can see
I am having a little trouble trying to accomplish this. Here is the gist
I'm having a little trouble getting an animated loading spinner to work for a
I'm having a little trouble using Hibernate with a char(6) column in Oracle. Here's
I'm having a little trouble with JQuery autocomplete remote-cache and Zend, it doesn't work
I'm having a little trouble with some code I'm writing. Basically, I'm trying to

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.