By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: Data Class (task with JS Class)

+3 votes

Write a JS class that holds data about an HTTP request. It has the following properties:

  • method (String)
  • uri (String)
  • version (String)
  • message (String)
  • response (String)
  • fulfilled (Boolean)

The first four properties (method, uri, version, message) are set trough the constructor, in the listed order. The response property is initialized to undefined and the fulfilled property is initially set to false.

Input / Output:

The constructor of your class will receive valid parameters. There is no output.

Submit the class definition as is, without wrapping it in any function.


Sample Input:

let myData = new Request('GET', '', 'HTTP/1.1', '')


let myData = new Request('GET', '', 'HTTP/1.1', '')    { method: 'GET',
  uri: '',
  version: 'HTTP/1.1',
  message: '',
  response: undefined,
  fulfilled: false }

asked in JavaScript category by user sam

1 Answer

+2 votes

My solution:

class Request {
    constructor(method, uri, version, message) {
        this.method = method;
        this.uri = uri;
        this.version = version;
        this.message = message;
        this.response = undefined;
        this.fulfilled = false;

let myData = new Request("GET", "", "HTTP/1.1", "");



Using ES6 syntax, a class can be defined similar to a function, using the class keyword.

At this point, the class can already be instantiated, but it won’t hold anything useful, since it doesn’t have a constructor. A constructor is a function that initializes the object’s context and attaches values to it. It is defined with the keyword constructor inside the body of the class definition and it follows the syntax of regular JS functions – it can take arguments and execute logic. Any variables we want to be attached to the instance must be prefixed with the this identifier.

The description mentions some of the properties need to be set via the constructor – this means the constructor must receive them as parameters. We modify it to take four named parameters that we then assign to the local variables.

Note the input parameters have the same names as the instance variables – this isn’t necessary, but it’s easier to read. There will be no name collision, because the this identifier tells the interpreter to look for a variable in a different context, so this.method is not the same as method.

answered by user nikole