サクサク読めて、アプリ限定の機能も多数!
トップへ戻る
2024年ランキング
dmitripavlutin.com
The ECMAScript modules system (import and export keywords) by default can import only JavaScript code. But it's often convenient to keep an application's configuration inside of a JSON file, and as result, you might want to import a JSON file directly into an ES module. For a long time, importing JSON was supported by commonjs modules format. Fortunately, a new proposal at stage 3 named JSON modul
Learning covariance and contravariance in TypeScript could be tricky (I know from my experience!), but knowing them is a great addition to understanding types and subtyping. In this post, you'll read an accessible explanation of covariance and contravariance concepts. 1. Subtyping Subtyping is a form of polymorphism in which a subtype is associated with a base type by some form of substitutability
Because callback param is of any type, the statement callback() won't trigger type errors. You can do anything with a variable of type any. But running the script throws a runtime error: TypeError: callback is not a function. 1 is a number and cannot be invoked as a function — and TypeScript hasn't protected you from this error! How to allow invokeAnything() function to accept any kind of argument
Is there any difference between using 'return await promise' and 'return promise' in asynchronous JavaScript functions?
When a React component handles bursting events like window resize, scrolling, user typing into an input, etc. — it's wise to soften the handlers of these events. Otherwise, if the handlers are invoked too often you risk making the application lagging or even unresponsive for a few seconds. In this regards, debouncing and throttling techniques can help you control the invocation of the event handle
Starting version 13.2.0, Node.js has stable support of ES modules. In this post, you'll learn how to enable and use ES modules in Node.js. 1. Enabling ECMAScript modules in Node.js The default format of modules in Node.js is the CommonJS. But Node.js will treat a JavaScript file as an ECMAScript modules format if: The module's file extension is .mjs Or the module's nearest parent folder has { "typ
In this post you'll learn how to use React.useRef() hook to create persisted mutable values (also known as references or refs), as well as access DOM elements. 1. Mutable values useRef(initialValue) is a built-in React hook that accepts one argument as the initial value and returns a reference (aka ref). A reference is an object having a special property current.
useEffect() hook manages the side-effects like fetching over the network, manipulating DOM directly, and starting/ending timers. Although the useEffect() is one of the most used hooks along with useState(), it requires time to familiarize and use correctly. A pitfall you might experience when working with useEffect() is the infinite loop of component renderings. In this post, I'll describe the com
The Fetch API is the default tool for performing network operations in web applications. Although fetch() is generally easy to use, there are some nuances to be aware of. In this post, you'll find the common scenarios of how to use fetch() with async/await syntax. You'll understand how to fetch data, handle fetch errors, cancel a fetch request, and more. 1. Intro to fetch() The Fetch API accesses
"Every callback function should be memoized to prevent useless re-rendering of child components that use the callback function" is the reasoning of his teammates. This reasoning is far from the truth. Such usage of useCallback() without profiling makes the component slower and increases code complexity. In this post, I'm going to explain how to use correctly useCallback(). 1. Understanding functio
State inside a React component is the encapsulated data that is persistent between renderings. useState() is the React hook responsible for managing state inside a functional component. I like that useState() indeed makes the work with state quite easy. But often I encounter questions like: should I divide my component's state into small states, or keep a compound one? if the state management beco
Using ES2015 modules you can chunk the application code into reusable, encapsulated, one-task focused modules. That's all good, but how do you structure modules? How many functions, classes a module should have? This post presents 4 best practices on how to organize better your JavaScript modules. 1. Prefer named exports When I started using JavaScript modules, I had used the default syntax to exp
The arrow function deserves the popularity. Its syntax is concise, binds this lexically, fits great as a callback. In this post, you'll read 5 best practices to get even more benefits from the arrow functions. 1. Arrow function name inference The arrow function in JavaScript is anonymous: the name property of the function is an empty string ''. The anonymous functions are marked as anonymous durin
Any programming language has functions that go beyond the basic usage. It happens thanks to a successful design and a wide area of problems it tries to solve. One such function in JavaScript is the Array.from(): a workhorse allowing lots of useful transformations on JavaScript collections (arrays, array-like objects, iterables like string, maps, sets, etc). In this post, I will describe 5 use case
Looking at my regular JavaScript code, I see that destructuring assignments are everywhere. Reading object properties and accessing array items are frequent operations. The destructuring assignments make these operations so much easier and concise. In this post, I will describe 5 interesting uses of destructuring in JavaScript, beyond the basic usage. 1. Swap variables The usual way to swap 2 vari
Users enjoy fast and responsive user interfaces (UI). A UI response delay of fewer than 100 milliseconds feels instant to the user. But a delay between 100 and 300 milliseconds is already perceptible. To improve user interface performance, React offers a higher-order component React.memo(). When React.memo() wraps a component, React memoizes the rendered output of the wrapped component and then sk
Because JavaScript is permissive, developers have the temptation to access uninitialized values. I'm guilty of such bad practice too. Often such risky actions generate undefined related errors: TypeError: 'undefined' is not a function TypeError: Cannot read property '<prop-name>' of undefined and alike type errors. JavaScript developers can understand the irony of this joke:
Coding like Shakespeare: Practical Function Naming Conventions A clear and meaningful prose is easy to read and follow. Everyone enjoys reading such prose. The same quality should apply to the source code. The way the developer expresses his thoughts through a programming language is important. Writing code is communication: with your teammates and yourself. Clean code practicing is a big topic. S
This story starts with a confession: I was afraid of Unicode for a long time. When a programming task required Unicode knowledge, I was searching for a hackable solution for the problem, without a good understanding of what I was doing. My avoidance continued until I faced a problem that required detailed Unicode knowledge. There was no way to apply situational solutions. After putting in some eff
The complexity of software applications is growing. The code quality is important in order to make the application stable and easily extensible. Unfortunately almost every developer, including myself, in his career faced with bad quality code. And it's a swamp. Such code has the following harmful characteristics: Functions are too long and do too many things Often functions have side effects that
The array is an ordered collection of objects or primitive types. It's hard to imagine an application that doesn't use this data structure. Working with arrays consists in the following phases: Initialize the array and setup the initial elements Access the array elements by index Add new elements Remove existing elements The current article covers the array initialization and setting the initial e
It's known that before ECMAScript 2015 JavaScript language was tricky. For simple scripts ES5 code works fine. But as soon as the application complexity grows, you start to feel that the lack of language constructs is making your code unpleasant. Because you have to implement the application requirements, you can't jump over the parts of code that are troublesome to implement in ES5 style. The pos
Before ECMAScript 2015 object literals (also named object initializers) in JavaScript were quite elementary. It was possible to define 2 types of properties: Pairs of property names and related values { name1: value1 } Getters { get name(){..} } and setters { set name(val){..} } for computed property values Sadly, the object literal possibilities match into a single example:
Variables in a program are everywhere. They are small pieces of data and logic that interact with each other: and this activity makes the application alive. In JavaScript, an important aspect of working with variables is hoisting, which defines when a variable is accessible. If you're looking for a detailed description of this aspect, then you're in the right place. Let's begin. 1. Introduction Ho
It is a pleasure to see the evolution of the programming language you code every day. Learning from mistakes, searching for better implementation, creating new features is what makes the progress from version to version. This is happening to JavaScript these years when ECMAScript 6 brings the language to a new level of usability: arrow functions, classes and a lot more. And this is great! One of t
1. The mystery of this this keyword has been a mystery for me for a long time. From a background like Java, PHP or other standard language, this is the instance of the current object in the class method. this cannot be used outside the method and such a simple approach does not create confusion. In JavaScript the situation is different: this is the context of a function invocation (a.k.a. executio
このページを最初にブックマークしてみませんか?
『Dmitri Pavlutin Blog』の新着エントリーを見る
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く