Skip to content

Instantly share code, notes, and snippets.

@Abhinay-g
Last active July 15, 2019 07:41
Show Gist options
  • Save Abhinay-g/3820d8a51f92e7ede74d340db38f1cb0 to your computer and use it in GitHub Desktop.
Save Abhinay-g/3820d8a51f92e7ede74d340db38f1cb0 to your computer and use it in GitHub Desktop.
Javascript: Variable hoisting
referance : https://scotch.io/tutorials/understanding-hoisting-in-javascript
concept: all the declaration of var , let , const , function declaration, class declaration is moved up in run time.
var: variable hoisted and get (initialized) default value of "undefined" [variable declared without var are added to global scope]
let/ const : variable is hoisted but not assigned to any value
function declaration: function declaration is hoisted to upward.
Note: function and variable with same name then there will be preferance given to variable assignment over but when variable declaration
only then preferance given to function declaration no matter what order they are defined
class decleration: class declaration is hoisted but it can not be used before its defenation as like [let/const] class is not initialized
so to use a class we must declare and assign before.
referance: https://stackoverflow.com/questions/35537619/why-are-es6-classes-not-hoisted
* classess are converted to IEFE and we can not create an instance on nested function as nested function is out of current scope
Question:
==============================================
var a = 1;
function b() {
a = 10;
return;
function a() {}
}
b();
console.log(a);
Answer: 1
Explanation:
function declaration function a(){} is hoisted first and it behaves like var a = function () {};. Hence in local scope variable a is created.
If you have two variables with same name (one in global another in local), local variable always get precedence over global variable.
When you set a = 10;, you are setting the local variable a , not the global one. Hence, the value of global variable remain same and you get, 1 in the log. ref: js hoisting/scope
Extra: If you didnt have a function named as "a", you will see 10 in the log.
======================================================
var myObject = {
price: 20.99,
get_price : function() {
return this.price;
}
};
var customObject = Object.create(myObject);
customObject.price = 19.99;
delete customObject.price;
console.log(customObject.get_price());
Answer: You will see 20.99
Explanation: This is very interesting question. When you create object.create(myObject) you are creating new object where the myObject will be the parent of the newly created object. Hence the price property will be at the parent.
========================================================================================
var num = 10,
name = "Addy Osmani",
obj1 = {
value: "first value"
},
obj2 = {
value: "second value"
},
obj3 = obj2;
function change(num, name, obj1, obj2) {
num = num * 10;
name = "Paul Irish";
obj1 = obj2;
obj2.value = "new value";
}
change(num, name, obj1, obj2);
console.log(num); // 10
console.log(name);// "Addy Osmani"
console.log(obj1.value);//"first value"
console.log(obj2.valuee);//"new value"
console.log(obj3.valuee);//"new value"
Primitive type (string, number, etc.) are passed by value and objects are passed by reference.
If you change a property of the passed object, the change will be affected.
However, you assign a new object to the passed object, the changes will not be reflected.
==========================================================================================
what is Currying
Curring is partial invocation of a function. Currying means first few arguments of a function is pre-processed and a function is returned.
The returning function can add more arguments to the curried function
function addBase(base){
return function(num){
return base + num;
}
}
var addTen = addBase(10);
addTen(5); //15
addTen(80); //90
addTen(-5); //5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment