Search This Blog

Q56-Q60

Q56. Closures are more related to prototyping or they are completely different concept in JavaScript?
Q57. How to select an element using its CSS class in JavaScript?
Q58. What is V8 engine?
Q59. What is module pattern in Javascript?'
Q60. Difference between Find and Filter in javascript?
-------------------------------------------------------------------------------------------------------------------------
Q56. Closures are more related to prototyping or they are completely different concept in JavaScript?

Answer:

You can achieve almost everything using prototype concept what we can achieve using closures. but Closures are consider to be more modern concept than prototype, Although implementing prototype are consider to be faster than closures.

Example of Prototype --
function Book(title) {
    this.title = title;
}

Book.prototype.getTitle = function () {
    return this.title;
};

var myBook = new Book('War and Peace');
alert(myBook.getTitle())

Example of Closure --
function Book(title) {
    var book = {
        title: title
    };
    book.getTitle = function () {
        return this.title;
    };
    return book;
}

var myBook = Book('War and Peace');

alert(myBook.getTitle())

https://stackoverflow.com/questions/3564238/object-oriented-javascript-with-prototypes-vs-closures
-------------------------------------------------------------------------------------------------------------------------
Q57. How to select an element using its CSS class in JavaScript?

Answer: 

There are two ways. either use queryselector method syntax or use document.getElementsByClassName method.

element = document.querySelector(selectors);
var el = document.querySelector(".myclass");

example
---HTML--
<body>
  <div class='div1'> this is div1 </div>
    <div class='div2'> this is div2 </div>

</body>


--JavaScript--
(function(){
var a = document.querySelector('.div2');
  a.style.backgroundColor = "red";
})();


--JavaScript--
(function(){
var a = document.getElementsByClassName('div2');
  a[0].style.backgroundColor = "red";

})();
-------------------------------------------------------------------------------------------------------------------------
Q58. What is V8 engine?

Answer:

V8 is the name of the JavaScript engine that powers Google Chrome. It's the thing that takes our JavaScript and executes it while browsing with Chrome.

V8 provides the runtime environment in which JavaScript executes. The DOM, and the other Web Platform APIs are provided by the browser.

The cool thing is that the JavaScript engine is independent of the browser in which it's hosted. This key feature enabled the rise of Node.js. V8 was chosen to be the engine that powered Node.js back in 2009, and as the popularity of Node.js exploded, 


https://nodejs.dev/learn/the-v8-javascript-engine

-------------------------------------------------------------------------------------------------------------------------
Q59. What is module pattern in Javascript?

Answer:
It is a commonly used Design Pattern which is used to wrap a set of variables and functions together in a single scope.

https://www.youtube.com/watch?v=SKBmJ9P6OAk&ab_channel=AllThingsJavaScript%2CLLC

-------------------------------------------------------------------------------------------------------------------------
Q60. Difference between Find and Filter in javascript?

Answer:
The find() method returns the first value that matches from the collection. Once it matches the value in findings, it will not check the remaining values in the array collection.

The filter() method returns all the matched values in an array from the collection. It will check all values in the collection and return the matched values in an array.

Example below: 

var requests = [{  
    App: 'Apple',  
    Count: 12  
}, {  
    App: 'Amazon',  
    Count: 5  
}, {  
    App: 'Microsoft',  
    Count: 2  
},
  {
    App: 'Apple',  
    Count: 11  
}];  

var obj1 = requests.find(function(item) {  
    return item.App == "Apple" ;
});

var obj2= requests.filter(function(item) {  
    return item.App == "Apple"; 
});

console.log(obj1); // Find will return only 1 element. 
console.log(obj2); // Filter will return both element. 

output of find -->

[object Object] {
 
App: "Apple",
 
Count: 12
}

Output of Filter -->

[[object Object] {
 
App: "Apple",
 
Count: 12
}, [object Object] {
 
App: "Apple",
 
Count: 11
}]

https://www.c-sharpcorner.com/blogs/find-vs-filter-in-javascrtipt
-------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment