javascriptnode.jsconstructorstrict

Constructor in Strict Mode


I've been searching a lot for an answer to this REALLY simple question, but I cannot find it:

" How to create constructors in strict mode ? "

fetch(`https://restcountries.com/v3.1/name/${country_name}`, )
.then(response => {

    if(! response.ok)
        throw new MyError(response.statusText, response.status)

[...]

.catch(err => {
    renderNotFound(err.message, err.code)

[...]

class MyError {
constructor(message, code) {
    this.message = message
    this.code = code
}}

I know the .this keyword resolves to undefined in strict node. So how can we work around it to create constructors?

Thanks in advance

EDIT: fixed a missing } in the class MyError


Solution

  • How to create constructors in strict mode?

    When using class syntax, constructor functions are always in strict mode, because the body of a class is always strict. (Most new kinds of scope defined in ES2015+ are strict by default.)

    I know the .this keyword resolves to undefined in strict node. So how can we work around it to create constructors?

    No workaround required. this is set to undefined when you call a function without doing anything to set this (for example: example()). When you use new, this is set to an instance of the thing being created before your constructor code is called (in a base constructor) or when you call super(/*...*/) (in a subclass constructor). (And when you do something else to set this (like example.method() or method.call(example)), this is set just like it is in loose mode.) Example:

    class MyError {
        constructor(message, code) {
            this.message = message
            this.code = code
        }
    }
    
    const x = new MyError("Ooops!", 42);
    console.log(x.message); // Ooops!
    console.log(x.code);    // 42


    Just to underscore the point that class bodies are in strict mode by default:

    class MyError {
        // Code here is strict, even though the script
        // context around it isn't
        static example() {
            console.log(this === undefined);
        }
    }
    
    const loose = {
        // Code here is loose like the script context around it
        example() {
            console.log(this === undefined);
        }
    };
    
    let f;
    
    f = MyError.example;
    f(); // true
    
    f = loose.example;
    f(); // false


    In a comment you've suggested that code doesn't work in strict mode. It should, assuming you have a } on your class. It does in this fiddle (can't do fetch in stack snippets, sadly):

        "use strict";
    
        class MyError {
            constructor(message, code) {
                this.message = message
                this.code = code
            }
        }
    
        fetch(`/this-will-404`, )
        .then(response => {
            if(! response.ok)
                throw new MyError(response.statusText, response.status)
        })
        .catch(err => {
            renderNotFound(err.message, err.code);
        });
    
        function renderNotFound(message, code) {
            console.log(`Not found: ${message} ${code}`);
        }