Tricky Parts of JavaScript [Part 1]

in #javascript6 years ago (edited)

JavaScript has been my forte from 2014 and I just love working with it. The way it handles the complex problems is just amazing and different type of programming it offers to users is incredible. There are some tricky part of JS that even an experienced JS dev might not know it. This is a series to explain the same and I hope the JS and programming enthusiasts are gonna like it.



Q1 - What should be the result of the following code?


if (function a(){}) {



 console.log(typeof a)



}


Ans - undefined, I know what you guys must be expecting i.e. function 

If we know how JavaScript works then we will know how the above lines of code is executed. It is as follows


function a() will be evaluated as eval(function a(){}) which is going to 
return true. and hence it is going to enter in the if statement but if you
notice the scope of a is only limited to the eval and it cannot go beyond
that. So outside the value of a is not defined  Whose type is undefined

I hope you got the answer.


Q2 - What is the biggest drawback of having private member functions in JS?


Ans - The biggest disadvantage of having private member functions in a class or a constructor is that every instance created will have separate private function created for that instance which is going to increase the memory consumption and hence very inefficient. Let's take an example to understand this:


var Car = function(name, color, price) {



 this.name = name;



 this.color = color;



 this.price = price;



 



 var increasePrice = function(priceIncrease) {



   this.price += priceIncrease;



 }



}


Now every instance created of that car will have a separate function increasePrice created for it which is going to increase the memory consumption.



Q3 - Write a function so that you can add three numbers as 

add(1)(2)(3); // ans -> 6


Ans - If you understand completely how returns work in reality then it should be pretty straight forward. Let's see 


function add (num1) {



 return function (num2) {



   return function (num3) {



     return num1 + num2 + num3



   }



 }



}


So there are two anonymous function inside our add function, one returning the other and ultimately the values. So the values are passed like a water falling in the steps. I hope you understand it.


If you have any questions regarding the above cases, let me know. I will help you understand it.


More CONTENT to COME...STAY TUNED!

@funnyman

Sort:  

Thanks for the good explanation.

Thanks. I am felling happy to help.

Thank you so much. I appreciate it.

Thanks man.

Thank you so much buddy.

Thanks. You give me inspiration to write more posts like this.

Thanks bud. Appreciate it.

THANKS

I am glad that you liked it. :)

Thank you for appreciating as a follower.

Thank you so much.

Thanks. You made my day.

Thanks

Very nice post.

Very informative post. love it.

I love the post. Keep it up

Thanks.

Thank you so much.

Thanks. Totally appreciate it.

Thanks for coming by.

Keep writing good posts.

Thank you :)

I loved it. Keep it up.

I appreciate it.

Great job. It would help many

You should work on the text formatting

Thank you so much. Totally appreciate it.

Thank you.

I like the way you present your post.

Keep it up. I like it.

How much experience do you have in JS field?

It is so much fun. Thank you.

Thanks for suggestions.

Thank you. I appreciate it.

Coin Marketplace

STEEM 0.33
TRX 0.11
JST 0.034
BTC 66579.21
ETH 3282.19
USDT 1.00
SBD 4.30