JavaScript: Understanding the Weird Parts, Section 6, Lecture 57


Because Q&A on Udemy on this course is quite dead I am posting my question here in hope to get some help/explanation.
I am a bit confused in Section 6, Lecture 57, when instructor says that if function is in any way returned there would be an “interruption” but this code below still works for me? It seems this doesnt work only if one puts object in return or something?

function Auto() {
   this.brand = "Fiat";
   this.year = "2011";
   return "This shouldnt work?!";
  var italianAuto = new Auto(); 

 // and one more confusion, why, after execution of code, in console appears a name (word) Auto? There should be just an object with its properties and methods (or empty if that would be a case), like usually, as here:

 var a = {
  name: "Sandra",
  lastname: "Bullock"
 console.log(a); // <- a console will show an object but not its (variable) name a, but in above case it shows name-word Auto, why, how?

Thank you in advance!

When you call Auto() it gets the value of anything that is returned from inside it, that’s how return works, it is the code after the return statement that is not executed

O, very fast response, uau, really appreciate it and thank you very much.
So, I understand that and that is why I am a bit confused, there should not be object returned but my string with text actually error because new creats and emty object and thus with returned string it should bump in some problem or? But I get an Auto object in console with its properties and I should get that only if I would remove my return line, isnt that how suppose to be?

Plus (it is not biggy but I am curious) I dont know why I get Auto name of object, it should only be and object without variable name?

So, in your second block:

var a = {
  name: 'Sandra',
  lastname: 'Bullock'

You are creating an object directly, using the ‘Object Literal’ syntax. And that is perfectly legitimate, works fine. Won’t show the class from which this object was instantiated, as it was not created from a class – hence the whole “object literal” thing.

The first, you create a class, Auto, and then use that to instantiate the object. So when you create the object by

var italianAuto = new Auto();

you are using the constructor,Auto, that you have defined. That constructor has two properties, brand and year. So why does the line return "This shouldnt work?!"; not get returned? Here’s the skinny from the MDN:

The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.The new keyword does the following things:

  1. Creates a blank, plain JavaScript object;
  2. Links (sets the constructor of) this object to another object;
  3. Passes the newly created object from Step 1 as the this context;
  4. Returns this if the function doesn’t return its own object.

So look at your constructor. An object is created, values are set, the constructor is set, and you return… a String. Not an object. So… the string is ignored, and the object you’ve created is assigned to italianAuto.

If you like, you can console.log(Auto() ); and see that it does return your string.

Ooooo yes, thank you very much! Now I see, it is purposely designed like that by JS, so string is ignored and when I put an object in return line instead of a string I get an “error”, OK, and thanks for name clarification as well, class name, yeah, now I get it, at least I am on better way to get there:)


1 Like

Doing great, and a very solid question. Keep it up!

Thank you mister, I appreciate it. I don`t know how can I repay you, maybe in the future there will be a “thing” I could do for you:)

Of course there is. Pay that knowledge forward. As you learn more and more, keep coming back here and finding questions you can help with. That will more than repay me.

Nice & really great community. OK, I will do that:)

1 Like