Search This Blog

Q26-Q30

Q26: What is the difference between block scope and function scope in javscript?
Q27. What are prototypes in java script?
Q28. What is prototype chaining in java script.
Q29. What is prototype Inheritance in Javascript? 
Q30. What make generators different from normal function in javascript?
--------------------------------------------------------------------------------------------------------------------------
Q26: What is the difference between block scope and function scope in javscript?

Answer 26:

In actual there are three types of scopes in javascript- Global, functional, block scope. 

var in javascript can be reassigned and updated. var variables are ‘function scope.’ It means they are only available inside the function they’re created in, or if not created inside a function, they are ‘globally scoped.’ If var is defined inside a function and I subsequently try to call it outside the function, it won’t work.

function setWidth(){
    var width = 100;
    console.log(width);
}
width;
// Returns:

Uncaught ReferenceError: width is not defined


>> Let and Const keywords has scope of block instead of functions. A block is a set of opening and closing brackets.  so for below exmple

var age = 100;
  if (age > 12){
    let dogYears = age * 7;
    console.log(`You are ${dogYears} dog years old!`);

  }

if we try to get value of dogYears outside the block i.e curly braces, it will give not defined error. 


https://medium.com/@josephcardillo/the-difference-between-function-and-block-scope-in-javascript-4296b2322abe
--------------------------------------------------------------------------------------------------------------------------
Q27. What are prototypes in java script?

Answer 27.

1) All JavaScript objects inherit their properties and methods from their prototype. 
2) Objects created using an object.create literal, or with new Object(), inherit from a prototype called Object.prototype.
3) In JavaScript, all objects have a hidden [[Prototype]] property that’s either another object or null.
4) The object referenced by [[Prototype]] is called a “prototype”.
https://www.w3schools.com/js/js_object_prototypes.asp
--------------------------------------------------------------------------------------------------------------------------
Q28. What is prototype chaining in java script.

Answer 28:

<<Need Better Answer>>
Changes to the Object prototype object are seen by all objects through prototype chaining, unless the properties and methods subject to those changes are overridden further along the prototype chain.  This provides a very powerful although potentially dangerous mechanism to override or extend object behavior

If you try to look up a key on an object and it is not found, JavaScript will look for it in the prototype. It will follow the "prototype chain" until it sees a null value. In that case, it returns undefined.
http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/

--------------------------------------------------------------------------------------------------------------------------
Q29. What is prototype Inheritance in Javascript? 

Answer 29:

<<Yet to answer>>
--------------------------------------------------------------------------------------------------------------------------
Q30. What make generators different from normal function in javascript?

Answer 30:

Generators are introduced in ES6.

1) Generators functions can be stop and restarted as many times you choose. unlike normal functions
2) Generators allows two way passing of message as it progress. While in normal functions you pass parameters in the beginning and expect out at the end. With Generators functions, you send messages out with each yield and you send message back with each restart.


No comments:

Post a Comment