Found 9150 Articles for Object Oriented Programming

What is the difference between 'throw new Error' and 'throw someObject' in javascript?

Ayush Gupta
Updated on 02-Dec-2019 05:34:13

549 Views

The difference between 'throw new Error' and 'throw someObject' in javascript is that throw new Error wraps the error passed to it in the following format −{    name: 'Error',    message: 'Whatever you pass in the constructor' }The throw someObject will throw the object as is and will not allow any further code execution from the try block, ie same as throw new Error.

How to remove the hash from window.location (URL) with JavaScript without page refresh?

Abdul Rawoof
Updated on 26-Aug-2022 11:54:02

3K+ Views

The replaceState() method replaces the current history entry with the state objects, title, and URL passed as method parameters. This method is useful when you want to update the current history entry's state object or URL in response to a user action. To remove the hash URL, use the history API's replaceState method to remove the hash location. Syntax The syntax for the replaceState() method is as follows − window.history.replaceState(stateObj, "unused", "url") The function will take three parameters. They are − stateObj − This parameter is associated with history entry which is passed to the replace method. This ... Read More

What are the differences between Deferreds, Promises and Futures in javascript?

Ayush Gupta
Updated on 02-Dec-2019 05:31:52

1K+ Views

Future is an old term that is same as promise.A promise represents a value that is not yet known. This can better be understood as a proxy for a value not necessarily known when the promise is created.A deferred represents work that is not yet finished. A deferred (which generally extends Promise) can resolve itself, while a promise might not be able to do so. This can also be thought of as a promise that'll always succeed only.A promise is a placeholder for a result which is initially unknown while a deferred represents the computation that results in the value.Read More

Difference between Bottom-Up Model and Top-Down Model

Kiran Kumar Panigrahi
Updated on 06-Sep-2023 10:32:47

43K+ Views

In System Design, there are two types of approaches followed namely, the Bottom-Up Model and the Top-Down Model. The bottom-up model is one in which the different parts of a system are designed and developed and then all these parts are connected together as a single unit. On the other hand, the top-down model is one in which the whole system is decomposed into smaller sub-components, then each of these parts are designed and developed till the completed system is designed. Read this article to find out more about the bottom-up model and the top-down model of system ... Read More

Difference between Association and Aggregation in Java

Mahesh Parahar
Updated on 28-Nov-2019 10:16:56

8K+ Views

AssociationAssociation in terms of objects refers to "has a" relationship between two related objects. For example, a employee has a communication address.class Employee {    String name;    Address communicationAddress; } class Address {    String address; }AggregationAggregation in terms of objects refers to "has a"+ relationship between two related objects. For example, a department has multiple employees. It refers to having a collection of child objects in parent class. For example:class Department {    String name;    List employees; } class Employee {    String name; }Sr. No.KeyAssociationAggregation1DefinitionAssociation refers to "has a" relationship between two classes which use each ... Read More

What is the reason to choose jasmine over jest?

Ayush Gupta
Updated on 27-Nov-2019 12:31:17

98 Views

There is no solid reason to choose jasmine over jest. Both are excellent libraries that have been around for a while and have an opinionated way of doing things that are quite similar. Jest is built on top of jasmine.One reason why you might choose jasmine over jest is that it is faster. (https://github.com/facebook/jest/issues/6694)

What is the use of sinon.js?

Ayush Gupta
Updated on 27-Nov-2019 12:21:19

527 Views

SinonJS provides stand-alone test spies, stubs and mocks. It is a library that we can use to create object mocks for unit testing.Spies − Fake functions that we can use to track executions.Stubs −Functions replacements from which we can return whatever we want or have our functions work in a way that suites us to be able to test multiple scenarios.Mocks −Fake methodsAll these objects help in unit testing our code.

How to create an object property from a variable value in JavaScript?

Ayush Gupta
Updated on 27-Nov-2019 12:20:26

9K+ Views

JS has 2 notations for creating object properties, the dot notation and bracket notation.To create an object property from a variable, you need to use the bracket notation in the following way −Exampleconst obj = {a: 'foo'} const prop = 'bar' // Set the property bar using the variable name prop obj[prop] = 'baz' console.log(obj);OutputThis will give the output −{    a: 'foo',    bar: 'baz' }ES6 introduces computed property names, which allow you to do −Exampleconst prop = 'bar' const obj = {    // Use a as key    a: 'foo',    // Use the value of prop ... Read More

How to terminate javascript forEach()?

Ayush Gupta
Updated on 27-Nov-2019 12:17:48

519 Views

You can't break from the forEach method and it doesn't provide to escape the loop (other than throwing an exception).You can use other functions like _.find from lodash instead −_.find − it breaks out of the loop when the element is found. For example,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 forEach. For example,Exampletry {    [1, 2, 3, 4].forEach((element) => {       // Check your condition here       if (element === 2) {          throw new Error();       }       // Do what you want with the elements here       // ...    }) } catch (e) {    // Do nothing. }

What is the use of window.location in javascript?

Abdul Rawoof
Updated on 26-Aug-2022 11:57:03

2K+ Views

Window.location is the location property of a window and it 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 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 entire URL ... Read More

Advertisements