Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when running example code for Javascript doc - String.match() #574

Closed
MEF4232 opened this issue May 10, 2021 · 7 comments · Fixed by #578
Closed

Error when running example code for Javascript doc - String.match() #574

MEF4232 opened this issue May 10, 2021 · 7 comments · Fixed by #578
Assignees

Comments

@MEF4232
Copy link

MEF4232 commented May 10, 2021

Was reading about String.match() and testing the example code snippets. The code snippet for Using named capturing groups does not work (see screenshot below)

Screen Shot 2021-05-10 at 1 05 56 PM

@MEF4232 MEF4232 changed the title Error when running given test code Error when running example code for Javascript doc - String.match() May 10, 2021
@wbamberg
Copy link
Collaborator

This is interesting. The same example works in the browser console, but fails in the interactive example's console, in both Firefox and Chrome.

Firefox: "Error: t.constructor is undefined"
Chrome: "Error: Cannot read property 'name' of undefined"

I think it's very likely that this is due to a bug in the interactive examples code that implements console.log: formatOutput is trying to work out what kind of thing it's trying to log, and if it thinks it's an object, calling formatObject. In particular it is doing:

var objectName = input.constructor.name;

...so it's assuming that if it's being called, then the argument has a constructor property. But in the browser console:

const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';

const capturingRegex = /(?<animal>fox|cat) jumps over/;
const found = paragraph.match(capturingRegex);

console.log(typeof found.groups); // "object"
console.log(typeof found.groups.constructor) // "undefined"
console.log(typeof found.groups.constructor.name) // "TypeError: found.groups.constructor is undefined"

@schalkneethling , what do you reckon?

@schalkneethling
Copy link

@schalkneethling , what do you reckon?

Will take a look. Thanks for the heads up, @wbamberg

@schalkneethling
Copy link

@MEF4232 Thank you for reporting this. @wbamberg Thank you for digging in, that is indeed the problem. As this is a problem with interactive examples/Bob I am moving the issue to the mdn/bob repo

@schalkneethling schalkneethling transferred this issue from mdn/content Jun 1, 2021
@schalkneethling schalkneethling self-assigned this Jun 1, 2021
@schalkneethling
Copy link

@wbamberg Ok, so adding this solves the problem:

if (!objectName.constructor || !objectName.prototype) {
    var formattedChild = '';
    var start = true;
    for (var key in input) {
        if (start) {
            start = false;
        } else {
            formattedChild = formattedChild + ', ';
        }
        formattedChild =
            formattedChild + key + ': ' + this.formatOutput(input[key]);
    }
    return 'Object { ' + formattedChild + ' }';
}

What I am really curious about is what type of Object this is that has neither a constructor nor a prototype. According to the docs:

All objects (with the exception of objects created with Object.create(null)) will have a constructor property

Are there other instances of this other than on String.match?

@karlcow
Copy link

karlcow commented Jun 9, 2021

Just to remember for later for myself (but if it's interesting to others)

@moztcampbell
Copy link

Searching the ECMAScript spec for uses of OrdinaryObjectCreate(null) I find a few:

  • @@unscopables properties such as [1,2,3][Symbol.unscopables]
  • import.meta in modules
  • The regexp named capture groups described in their issue

So it is very uncommon.

@schalkneethling
Copy link

Thank you for the extremely valuable info @karlcow and @moztcampbell ❤️

schalkneethling pushed a commit that referenced this issue Aug 25, 2021
* fix: add support for special object types in console

Correctly print the value of objects such as those returned by named capture groups

fix #574

* fix failing test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants