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

Introducing Constructors #32488

Closed
BigShuff opened this issue Feb 29, 2024 · 3 comments · Fixed by #32491
Closed

Introducing Constructors #32488

BigShuff opened this issue Feb 29, 2024 · 3 comments · Fixed by #32491
Labels
Content:Learn:JavaScript Learning area JavaScript docs needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened.

Comments

@BigShuff
Copy link

MDN URL

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics

What specific section or headline is this issue about?

The example that follows this line "Constructors, by convention, start with a capital letter and are named for the type of object they create. So we could rewrite our example like this: JS"

What information was incorrect, unhelpful, or incomplete?

Please understand I don't feel confident enough to say that the following is definitely an error but for the life of me I cant see that that the code is required. And therefore found it confusing. I have put the code in question in bold.
Note this constructor is as I see it fine the each new object will be the value of the argument.
function createPerson(name) {
const obj = {};
obj.name = name;
obj.introduceSelf = function () {
console.log(Hi! I'm ${this.name}.);
};
return obj;
}

The bold code seems to have no meaning?
const salva = createPerson("Salva");
salva.name;
salva.introduceSelf();
// "Hi! I'm Salva."

const frankie = createPerson("Frankie");
frankie.name;
frankie.introduceSelf();
// "Hi! I'm Frankie."

What did you expect to see?

I don't think the code has any meaning even when removed the names are still given to each object

Do you have any supporting links, references, or citations?

This code with the lines in question commented out still produces the same result.

function createPerson(name) {
const obj = {};
obj.name = name;
obj.introduceSelf = function () {
console.log(Hi! I'm ${this.name}.);
};
return obj;
}

const salva = createPerson("Salva");
//salva.name;
salva.introduceSelf();
// "Hi! I'm Salva."

const frankie = createPerson("Frankie");
//frankie.name;
frankie.introduceSelf();
// "Hi! I'm Frankie."

Do you have anything more you want to share?

No response

MDN metadata

Page report details
@BigShuff BigShuff added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Feb 29, 2024
@github-actions github-actions bot added the Content:Learn:JavaScript Learning area JavaScript docs label Feb 29, 2024
@wbamberg
Copy link
Collaborator

You're right, those 2 lines should be removed. Well spotted! Would you like to file a PR for it?

@BigShuff
Copy link
Author

BigShuff commented Feb 29, 2024 via email

@wbamberg
Copy link
Collaborator

Not at all, it's fine :). Thank you for finding the error and taking the time to file a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:Learn:JavaScript Learning area JavaScript docs needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants