Object Creation from function in browser

When we create an object from a function in browser using the below code

function myFunction() {
  this.name = 'hello';

const myFun = myFunction();

Due to lack of new keyword , will variable myFun be assigned to correct object and this.name remains undefined or window.name is assigned with “hello” , but variable myFun remains undefined . Can anyone clarify on this ?

This is another homework question. You have an invalid function in your example because it has no parameter list. I recommend that you log the this keyword, to learn what happens with it from there.


Just to point out, you can select/highlight your text in the editor and click on the </> preformatted button (ctrl-e) to format it as code.

This will add three opening and closing ticks ``` around your text/code and make it easier to read.


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.