54
u/Wtaurus Aug 19 '21
Do you mean logging the objects you created?
console.log(berkeleyStudent.displayInfo())
console.log(losAngelesStudent.displayInfo())
67
u/benranger Aug 19 '21
Mfs named Brayan
37
u/jaredcheeda Aug 19 '21
This is my oldest, Brayan and his sister Merlissa, and my youngest Narthan.
3
9
u/FriesWithThat Aug 19 '21
We need more Brayan license plates in the Gift Shop. Repeat, we are sold out of Brayan license plates.
8
u/FriesWithThat Aug 19 '21
Taking that question literally:
console.log(
`berkleyStudent.constructor is ${berkelyStudent.constructor}`,
);
/*
berkleyStudent.constructor is function Student(name, university) {
this.name = name;
this.university = university;
this.displayInfo = function () {
return `${this.name} attends ${this.university}`;
};
}
*/6
2
Aug 19 '21 edited Sep 29 '25
vast chase rain important yam sip fall deserve beneficial squeeze
This post was mass deleted and anonymized with Redact
1
u/electron_myth Aug 19 '21
(new Student()).constructor
yes this works
5
u/skerit Aug 19 '21
Student.prototype.constructor is a reference to Student, so there's no need to create an instance just to get the constructor.
1
Aug 19 '21 edited Sep 29 '25
consist station angle start attraction grab marble handle serious merciful
This post was mass deleted and anonymized with Redact
2
Aug 19 '21 edited Sep 29 '25
plants sparkle steer saw special cover smile whole kiss judicious
This post was mass deleted and anonymized with Redact
2
u/FlippySquirrel Aug 19 '21
There's more than one console command, and selecting a different one might get you better results.
Apologies is this is a pedantic response.
https://developer.mozilla.org/en-US/docs/Web/API/Console
In particular, I'm thinking of console.dir()
2
2
Aug 19 '21
In this thread: a dozen people massively overthinking it.
function Student(name, university) {
//etc
}
console.log(Student);If the above doesn't work, there's some additional context to your code that you haven't shared.
2
u/TheDaily-TechTalk Aug 19 '21 edited Aug 19 '21
I've just wrote article about console object. You might find it useful. Link to article
1
u/Thonk_Thickly Aug 19 '21
You can spoof it since you know the constructor’s signature
functionLogStudentCtor(student) {
console.log(`new Student(${student.name},${student.university})`)
}1
1
1
u/chetnrot Aug 19 '21
What is the difference between declaring Student as a function vs a class? I'd imagine since student is a new type of object, it'd be declared as a class, but I've seen also using "function" instead like in this picture.
1
u/A4_Ts Aug 19 '21
One of the quirks of Javascript
1
u/chetnrot Aug 19 '21
Is there a difference?
1
u/A4_Ts Aug 19 '21
For javascript, I don’t think so. Never actually seen classes used in pure JS
2
u/electron_myth Aug 19 '21
They do have classes where you can inherit with 'extends', and then use 'super()' in the constructor
3
Aug 19 '21
And that's syntax sugar.
Classes were added much after JavaScript was invented.
It was added to help other people who came from other languages.
1
1
1
1
u/TheUruz Aug 19 '21
isn't the "this" keyword supposed to be used in a class constructor? isn't this a function and therefore the "this" keyword is referring to the function itself?
2
u/zerik100 Aug 19 '21
The ES6 `class` keyword is just syntactic sugar to make JS look more similar to classic OOP programming. OP's example is a valid constructor function that could be rewritten as:
class Student { constructor (name, university) { this.name = name this.university = university } displayInfo () { return this.name + " attends " + this.university } }1
u/TheUruz Aug 19 '21
oh god i'm not that much into js but that sounds pretty traumatic
2
u/zerik100 Aug 19 '21
The `class` keyword is certainly the preferred way of doing it nowadays, writing your own constructor functions is rarely, if ever, needed and not recommended. OP's example is just the way it was done before ES6 was introduced (dark times).
1
Aug 19 '21
How about adding toString() method to the object Student? You can console.log the object and get whaterver you want because the instance of the object will be type converted to string?
-4
u/the0melette Aug 18 '21
console.log(Student())
And you can pass in the parameters.
8
u/senocular Aug 18 '21
console.log(Student())
This would log undefined or throw an error. As a constructor, Student should be called with
newas seen withberkeleyStudentandlosAngelesStudent. If not, in sloppy mode it would pollute global with properties intended for new student instances and return undefined (since there's no explicit return), or in strict mode throw an error sincethiswould be undefined and attempting to set a property on undefined would result in a TypeError.If adding
newthen this would log an instance of Student, not the Student constructor. Assuming OP actually wants the constructor, that would simply be a matter of loggingStudentwithout the parens (though it's probably not what they really want).
13
u/senocular Aug 18 '21
You mean
?