Introducing Constructors #32488
Labels
Content:Learn:JavaScript
Learning area JavaScript docs
needs triage
Triage needed by staff and/or partners. Automatically applied when an issue is opened.
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
en-us/learn/javascript/objects/basics
The text was updated successfully, but these errors were encountered: