Новая статья: Раннее и позднее связывание в JavaScript

Дата публикации: 2024-11-12

Опубликовал статью на Medium и Habr, где делюсь своим взглядом на различия между ранним и поздним связыванием в JavaScript.

Через практические примеры я объясняю, как эти подходы влияют на структуру кода, и как они связаны с концепцией внедрения зависимостей. Если вы стремитесь создавать более гибкие и масштабируемые архитектуры — эта статья будет полезна.

Надеюсь, она прояснит ключевые моменты и вдохновит на применение новых подходов в проектах.

Дополнительные фрагменты исходного кода

export class Cat {
speak(): void {
console.log(“Meow”);
}
}
import {Cat} from “./cat”; export function animalSound(animal: Cat): void {
animal.speak();
}
import {animalSound} from ‘./animal’;
import {Cat} from ‘./cat’; const myCat = new Cat();
animalSound(myCat);
import {Cat} from “./cat”;
import {Dog} from “./dog”; export function animalSound(animal: Cat | Dog): void {
animal.speak();
}
export interface Animal {
speak(): void;
}
import {Animal} from “./iAnimal”; export function animalSound(animal: Animal): void {
animal.speak();
}
import {Animal} from ‘./iAnimal’; export class Cat implements Animal {
speak(): void {
console.log(“Meow”);
}
}
import {animalSound} from ‘./animal’;
import {Cat} from ‘./cat’; const myCat = new Cat();
animalSound(myCat);
import {Animal} from ‘./animal’; export class Dog implements Animal {
speak(): void {
console.log(“Woof”);
}
}
export interface Animal {
speak(): void;
} import {Animal} from “./iAnimal”;
export function animalSound(animal: Animal): void {
animal.speak();
}
export {}; export function animalSound(animal) {
animal.speak();
}
class IAction {
act(opts) {}
}
export class FindUser {
act(opts) {}
}
export function animalSound(animal) {
animal.speak();
} We can see that the animalSound function relies on (or depends on) the animal object. However, there are no import statements linking this function to any specific implementation elsewhere in the code.
export default function (
{
}
) {}