Tricky Parts of JavaScript [Part 1]

in javascript •  4 months ago

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

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thanks for the good explanation.

·

Thank you so much.

·

Thanks. I am felling happy to help.

·

Thanks man.

·

Thank you so much buddy.

·

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

·

Thanks bud. Appreciate it.

·

THANKS

·

Thank you so much. I appreciate it.

·

Thanks. You made my day.

·

I am glad that you liked it. :)

Thanks

Very nice post.

Very informative post. love it.

I love the post. Keep it up

·

Thanks. Totally appreciate it.

·

Thanks for coming by.

·

I appreciate it.

·

Thanks.

·

Thank you so much.

Great job. It would help many

You should work on the text formatting

·

Thanks for suggestions.

·

Thank you. I appreciate it.

·

Thank you so much. Totally appreciate it.