Found 9150 Articles for Object Oriented Programming

How to break the _.each function in underscore.js

Ayush Gupta
Updated on 02-Dec-2019 06:45:59

329 Views

You can't break from each method. It copies the native forEach method's behavior and the native forEach doesn't provide to escape the loop (other than throwing an exception).You can use other functions like −_.find: it breaks out of the loop when the element is found. For example,_.find([1, 2, 3, 4], (element) => {    // Check your condition here    if (element === 2) {       return true;    }    // Do what you want with the elements here    // ... });Throw an exception from each. For example,try {    _([1, 2, 3, 4]).each((element) => {       // Check your condition here       if (element === 2) {          throw new Error();       }       // Do what you want with the elements here       // ...    }) } catch (e) {    // Do nothing. }

JavaScript variables declare outside or inside loop?

Abdul Rawoof
Updated on 26-Aug-2022 11:56:41

5K+ Views

It is believed that there can be any performance-based differences when the variables are declared inside or outside the loops, but there will be no difference. As there is flexibility in any programming language to declare the variables as and when required which will be easy for the users to read and understand accordingly. So, in JavaScript too the variables can be declared inside or outside the loop as per the users ease anywhere in the function body. And even variable declarations are not commands that are executed at the run-time. If the variable is declared outside the loop, then ... Read More

How to add a class to DOM element in JavaScript?

Abdul Rawoof
Updated on 26-Aug-2022 11:55:32

1K+ Views

HTML is a mark-up language used to create the useful elements of a webpage. The DOM which means Document Object Model is used to manipulate the HTML Document. In DOM, all the elements are defined as objects. The styling in the HTML document can be done by using CSS. In here, JavaScript is being used to manipulate them. The ways in which the class can be added to a class to the DOM elements are described below. Using className If the element has a class already, then it will just add another class to it if not the element gets ... Read More

What is the use of sentry in javascript?

Ayush Gupta
Updated on 02-Dec-2019 06:25:48

363 Views

Sentry is a complete javascript debugging and monitoring tool package that allows you to track your production code. Some of the features of sentry −Record environment and usage details to recreate and fix bugsSee the error and stack trace previously only visible in user's debug console.Apply source maps automatically to convert minified, compiled, or transpiled code back to its original form.Mobile app reporting support.

How can you detect the version of a browser using Javascript?

Aman Kumar
Updated on 19-Dec-2022 11:16:05

5K+ Views

In this article, we are going to discuss how to detect the version of a browser using JavaScript. These days, most browsers are JavaScript−enabled. But still, there are some browsers that don’t support JavaScript; or, some versions of some browsers don’t support certain features of JavaScript. Therefore, in certain instances, there is a need to know the details of the client’s web browser so that the applications can deliver appropriate content. Detecting the Version of a Browser You can detect the details of the current browser using navigator.appName and navigator.appVersion properties. To detect the version of the browser in the ... Read More

Proper way to catch exception from JSON.parse

Ayush Gupta
Updated on 02-Dec-2019 06:17:05

4K+ Views

The best way to catch invalid JSON parsing errors is to put the calls to JSON.parse() to a try/catch block.Examplefunction parseJSONSafely(str) {    try {       return JSON.parse(str);    }    catch (e) {       console.err(e);       // Return a default object, or null based on use case.       return {}    } }

What's the difference between window.location and document.location?

Abdul Rawoof
Updated on 26-Aug-2022 11:56:00

2K+ Views

The window.location property The location property of a window (i.e. window.location) is a reference to a Location object; it represents the current URL of the document being displayed in that window. Since window object is at the top of the scope chain, so properties of the window.location object can be accessed without the window prefix. For example window.location.href can be written as location.href. The following section will show you how to get the URL of page as well as hostname, protocol, etc. using the location object property of the window object. You can use the window.location.href property to get the ... Read More

Difference between application/x-javascript and text/javascript content types?

Ayush Gupta
Updated on 02-Dec-2019 06:07:38

1K+ Views

The text/javascript content type is obsolete. This was used in the early days of Html.application/x-javascript was an experimental content type(hence the x-). You should not be using this in your application.The correct content type to use in browsers is application/javascript. This helps browsers accept the content as js code.

What is the replacement of lodash pluck() method?

Ayush Gupta
Updated on 02-Dec-2019 06:06:54

1K+ Views

Pluck has been removed from lodash 4. This is because it did the same thing as a map.As a replacement, you can use the map function in the following way −Exampleimport _ from 'lodash' const objects = [{ 'a': 1 }, { 'a': 2 }]; console.log(_.map(objects, 'a'))OutputThis will give the output −[1, 2]

Where is _.pluck() in lodash version 4?

Ayush Gupta
Updated on 02-Dec-2019 06:06:00

430 Views

Pluck has been removed from lodash 4. This is because it did the same thing as a map.As a replacement, you can use the map function in the following way −Exampleimport _ from 'lodash' const objects = [{ 'a': 1 }, { 'a': 2 }]; console.log(_.map(objects, 'a'))This will give the output −Output[1, 2]

Advertisements