Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
What is use of static method in javaScript? And how I clear this error?

Write a class called Group (since Set is already taken). Like Set, it has add, delete, and has methods. Its constructor creates an empty group, add adds a value to the group (but only if it isn’t already a member), delete removes its argument from the group (if it was a member), and has returns a Boolean value indicating whether its argument is a member of the group.

Use the === operator, or something equivalent such as indexOf, to determine whether two values are the same.

Give the class a static from method that takes an iterable object as argument and creates a group that contains all the values produced by iterating over it.

This the question I need to solve and below is my code which gives error.

The error here is :

Uncaught TypeError: this.add is not a function

I don't know why this static method is needed here please let me know. And help me clear this error.

class Group {
  constructor() {
    this.group = [];
    return this;
  }

  add(value) {
    if (!this.has(value)) {
      this.group.push(value);
      return this;
    }
  }

  delete(value) {
    if (this.has(value)) {
      this.group = this.group.filter(x => x !== value)
      return this;
    }
  }

  has(value) {
    return this.group.includes(value)
  }

  static from(iterableObject) {
    for (let value of iterableObject) {
      this.add(value);
    }
    return this;
  }
}

let group = Group.from([10, 20]);
console.log(group.has(10));
// → true
console.log(group.has(30));
// → false
group.add(10);
group.delete(10);
console.log(group.has(10));
//→ false

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The objective is:

Give the class a static from method that takes an iterable object as argument and creates a group that contains all the values produced by iterating over it.

So, inside the method, you need to

creates a group

which can be done with new Group, then

contains all the values produced by iterating over it

with the loop referencing that newly created group, and then return the created group.

  static from(iterableObject) {
    const group = new Group();
    for (const value of iterableObject) {
      group.add(value);
    }
    return group;
  }

class Group {
  constructor() {
    this.group = [];
    return this;
  }
    
  add(value) {
    if (!this.has(value)) {
      this.group.push(value);
      return this;
    }
  }
  
  delete(value) {
    if (this.has(value)) {
      this.group = this.group.filter(x => x !== value)
      return this;
    }
  }
  
  has(value) {
    return this.group.includes(value)
  }
  
  static from(iterableObject) {
    const group = new Group();
    for (const value of iterableObject) {
      group.add(value);
    }
    return group;
  }
}

let group = Group.from([10, 20]);
console.log(group.has(10));
// → true
console.log(group.has(30));
// → false
group.add(10);
group.delete(10);
console.log(group.has(10));
//→ false

I don't know why this static method is needed here

A static method can be thought of as being similar in appearance to a standalone function (in terms of how it's called) - except that it's a property of the class itself. You could equivalently do

const createGroupFrom = (iterable) => {
  // implementation here
};

But being static usually indicates a function that is related to the class, without being tied to one particular already-existing instance. (Methods that are directly related to working on already-existing instances are implemented as non-static, regular methods.

In this situation, a regular method clearly wouldn't work, because it wouldn't be called on an existing instance - an existing instance doesn't exist. So the options are to have a plain standalone function, or a static method, if one prefers it - and the requirement given is to use a static method.

about 4 years ago · Juan Pablo Isaza Report

0

As stated on MDN:

The static keyword defines a static method or property for a class, or a class static initialization block (see the link for more information about this usage). Neither static methods nor static properties can be called on instances of the class. Instead, they're called on the class itself.

In the static method you have,

  static from(iterableObject) {
    for (let value of iterableObject) {
      this.add(value);
    }
    return this;
  }

But what you'll want to do is set a reference to a new Group instance and return that instance:

class Group {
  constructor() {
    this.group = [];
    return this;
  }

  add(value) {
    if (!this.has(value)) {
      this.group.push(value);
      return this;
    }
  }

  delete(value) {
    if (this.has(value)) {
      this.group = this.group.filter(x => x !== value)
      return this;
    }
  }

  has(value) {
    return this.group.includes(value)
  }

  static from(iterableObject) {
    var newGroup = new Group(iterableObject);
    for (let value of iterableObject) {
      newGroup.add(value);
    }
    return newGroup;
  }
}

let group = Group.from([10, 20]);
console.log(group.has(10));
// → true
console.log(group.has(30));
// → false
group.add(10);
group.delete(10);
console.log(group.has(10));
//→ false

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!