Object Oriented Programming Articles

Page 90 of 589

Safely setting object properties with dot notation strings in JavaScript

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 1K+ Views

In JavaScript, safely setting nested object properties can be challenging because accessing undefined nested paths throws errors. This article covers two approaches: using Lodash's set method and creating a custom solution. Using Lodash's set() Method Lodash provides a safe set method that handles nested property assignment without throwing errors, even if intermediate properties don't exist. let _ = require("lodash"); let obj = { a: { b: { foo: "test" ...

Read More

Jasmine JavaScript Testing - toBe vs toEqual

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 910 Views

In Jasmine JavaScript testing, toBe and toEqual are two different matchers for comparing values. Understanding their difference is crucial for writing effective tests. toBe vs toEqual Overview Arrays and objects can be compared in two ways: Reference equality: They refer to the same object in memory Value equality: They may refer to different objects but their contents are identical Using toBe (Reference Equality) The toBe matcher checks if two variables reference the exact same object in memory. It uses JavaScript's === operator internally. ...

Read More

JavaScript Encapsulation using Anonymous Functions

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 457 Views

Object-oriented programming languages allow data hiding using private fields. They use these to hide the internals of classes. In JavaScript there is no built-in support to hide/encapsulate the inner workings, but we can achieve encapsulation using anonymous functions. Anonymous functions, particularly when used as Immediately Invoked Function Expressions (IIFEs), can create private scopes that prevent global namespace pollution and provide encapsulation. The Problem: Global Namespace Pollution When we declare variables and functions in the global scope, they become accessible everywhere and can cause naming conflicts: const HIDDEN_CONST = 100; function fnWeWantToHide(x, y) { ...

Read More

What blocks Ruby, Python to get Javascript V8 speed?

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 164 Views

Nothing technically prevents Ruby and Python from achieving JavaScript V8 speeds. The performance gap exists primarily due to differences in optimization investments and language design choices rather than fundamental limitations. The V8 Advantage Google's V8 engine benefits from massive engineering resources and specific optimizations: Just-In-Time (JIT) compilation: Converts JavaScript to optimized machine code at runtime Hidden class optimization: Optimizes object property access patterns Inline caching: Speeds up method calls and property lookups Garbage collection tuning: Minimizes pause times during memory cleanup Ruby and Python Constraints Several factors limit Ruby and Python performance compared ...

Read More

How to programmatically set the value of a select box element using JavaScript?

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 1K+ Views

We can set the value of a select box using JavaScript by accessing the element and changing its value property. This is useful for dynamically updating form selections based on user interactions or application state. HTML Setup First, let's create a select element with multiple options: Select Apple Strawberry Cherry Guava Method 1: Using querySelector and value Property The most common approach is to use querySelector to find the element and ...

Read More

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

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 483 Views

The _.pluck() method was removed from lodash version 4 because it provided the same functionality as _.map(). This change was part of lodash's effort to reduce redundancy and improve consistency. What _.pluck() Did In lodash 3.x, _.pluck() extracted property values from a collection of objects: // Lodash 3.x syntax (no longer available) _.pluck(objects, 'propertyName') Replacement: Using _.map() In lodash 4+, use _.map() with a property path string to achieve the same result: const _ = require('lodash'); const objects = [{ 'a': 1 }, { 'a': 2 }, { 'a': 3 }]; ...

Read More

How to delete a localStorage item when the browser window/tab is closed?

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 2K+ Views

To clear localStorage data when a browser window or tab is closed, you can use the beforeunload or unload events. However, there are important limitations and better alternatives to consider. Using beforeunload Event The beforeunload event fires before the page unloads, giving you a chance to clean up localStorage: window.addEventListener('beforeunload', function() { // Clear specific item localStorage.removeItem('userSession'); // Or clear all localStorage localStorage.clear(); console.log('localStorage cleared ...

Read More

What is the replacement of lodash pluck() method?

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 2K+ Views

Lodash's pluck() method was removed in version 4.0 because it provided the same functionality as the map() method. The pluck() method was used to extract property values from objects in an array. Using _.map() as Replacement You can replace _.pluck() with _.map() using the property shorthand syntax: import _ from 'lodash'; const objects = [{ 'a': 1 }, { 'a': 2 }, { 'a': 3 }]; console.log(_.map(objects, 'a')); [1, 2, 3] Using Native Array.map() For modern JavaScript, you can use the native Array.map() method without lodash: const objects ...

Read More

How can I trigger an onchange event manually in javascript?

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 28K+ Views

In JavaScript, you can manually trigger an onchange event using the dispatchEvent() method with a custom Event object. This is useful for programmatically simulating user interactions. Basic Setup First, let's create an input element with a change event listener: document.querySelector('#test').addEventListener('change', () => { console.log("Changed!"); }); Method 1: Using Event Constructor Create a new Event object and dispatch it on the target element: Trigger Change document.querySelector('#example1').addEventListener('change', () => { console.log("Change event fired!"); }); function triggerChange1() { ...

Read More

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

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 1K+ Views

When serving JavaScript files, choosing the correct MIME type is crucial for proper browser handling. Let's explore the differences between these content types and understand which one to use. text/javascript (Obsolete) The text/javascript content type was used in the early days of HTML but is now obsolete according to RFC 4329. While browsers still support it for backward compatibility, it should not be used in modern applications. // Server header (obsolete) Content-Type: text/javascript application/x-javascript (Experimental) application/x-javascript was an experimental content type, indicated by the "x-" prefix. The "x-" denotes non-standard or experimental MIME ...

Read More
Showing 891–900 of 5,881 articles
« Prev 1 88 89 90 91 92 589 Next »
Advertisements