JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – Function can be used as class using new operator

JavaScript Quiz – Function can be used as class using new operator

new operator –

The new operator is used to create an instance of object type which is defined by user. And also for one of the built-in object types that has a constructor function.

It’s syntax looks like –

new constructor[([arguments])]

For more detail information on javascript new operator click here.

JavaScript Question

In this javascript quiz code, you will need to find output of key value after using new keyword with function.

Firstly, we create a function with name get and it’s a parameterized function means it receives parameter. which are name and website?

function get(name, website){ … }

And inside of the function, we are assigning this name and website to this object. For example, it look like

this.name = name;

this.website = website;

new operator with function –

However, in the second step, we create a variable obj variable using let keyword and assign an instance of function get using the new operator and pass two values as parameter.

let obj = new get(“JS”, “jstartup.com”);

Finally, we are now console log two values which is given below.

console.log(obj.name); // output = ?

console.log(this.website); // output = ?

Any guess what will be the output ?

Answer – “JS“, undefined

Click To Run Code

Explanation

Now, you the answer is JS, undefined but why this. Let me explain

So, we create an instance of a function get and pass two values as parameter. It stores as an object in the obj variable. And if you console log obj variable. It looks like an object.

{ name : “JS”, website : “jsstartup.com” }

As a result, we get the first console log “JS” as the name key in the obj variable has “JS” as value.

Now, you are thinking about the other console log which is this.website.

As we use this.website in function but we never call a function due to which, this can not access it’s valued globally.

But, We create an instance of that function using the new operator. That’s why we get undefined.

Note:

If we call function normally with a passing parameter, then this has value of name and website. And they are globally accessible.

At last, That’s why we get an answer “JS, undefined“.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JS Quiz – Did really the reference pass using Array.from() method
  2. JS Quiz – Is the String() and toString() are really same
  3. JavaScript Quiz – can in operator check value or index in an array
  4. JavaScript Quiz – can really instanceof operator work on string literal

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link