Update some pieces

This commit is contained in:
Angelos Chalaris
2023-05-21 16:12:35 +03:00
parent 0c90a6a2bf
commit 165b765809
6 changed files with 20 additions and 17 deletions

View File

@ -6,7 +6,7 @@ language: javascript
tags: [string,regexp]
author: chalarangelo
cover: taking-photos
excerpt: Regular expressions are very powerful, but hard to master. Understand these features and start using them in your JavaScript code.
excerpt: Learn how to effectively use JavaScript regular expressions with these 6 powerful features.
dateModified: 2021-06-12T19:30:41+03:00
---

View File

@ -1,18 +1,18 @@
---
title: Typechecking objects with Proxy in JavaScript
shortTitle: Object typechecking with Proxy
shortTitle: Object type checking with Proxy
type: story
language: javascript
tags: [object,type,proxy]
author: chalarangelo
cover: customs
excerpt: A simple way to typecheck objects at runtime using the Proxy object.
excerpt: Learn how to type check objects at runtime using the powerful Proxy object in JavaScript.
dateModified: 2023-04-23T05:00:00-04:00
---
A while back, I was working on a project where some objects had **rigid structure requirements**. As I was really not in the mood to use TypeScript, I decided to create a typechecking mechanism for objects using the `Proxy` object.
Drawing inspiration from React's `PropTypes`, I created a handful of **type checking functions** for the most common types.
Drawing inspiration from React's `PropTypes`, I created a handful of **typechecking functions** for the most common types.
```js
const bool = v => typeof v === 'boolean';
@ -91,4 +91,4 @@ proxiedObj.birthday = null;
proxiedObj.whatever = 'something';
```
As you can see, `createShapeCheckerProxy` can be used with a plain object to create a reusable function that wraps an object with a typechecking `Proxy`. The defined `types` are used to typecheck individual properties and could be extended to support more complex types and special rules. Overall, this can be a pretty useful tool for **typechecking objects at runtime**, without having to use TypeScript or similar tools.
As you can see, `createShapeCheckerProxy` can be used with a plain object to create a reusable function that wraps an object with a type checking `Proxy`. The defined `types` are used to typecheck individual properties and could be extended to support more complex types and special rules. Overall, this can be a pretty useful tool for **typechecking objects at runtime**, without having to use TypeScript or similar tools.

View File

@ -6,13 +6,13 @@ language: javascript
tags: [type,variable]
author: chalarangelo
cover: boat-port
excerpt: Hoisting comes up a lot during JavaScript interviews. It's a concept that can require some getting used to, so read our guide to learn more.
excerpt: Hoisting comes up a lot during JavaScript interviews. It's a concept that may require some getting used to, so read our guide to learn more.
dateModified: 2021-06-12T19:30:41+03:00
---
Before your JavaScript code is executed, it is first parsed and compiled. During the _compile_ phase, variable and function declarations are put into memory, which is called **hoisting**.
Note that only declarations are hoisted, not initializations, meaning that if a variable is declared and initialized after using it, its value will not be initialized. This is an oversimplification of the situation, so let's take a look at the different cases:
It's essential to note that only declarations are hoisted, while initializations are not. This means that if you declare and initialize a variable after using it, its value will not be initialized. However, this is a simplified explanation, so let's take a look at the various scenarios:
### function