其他
前端框架自欺欺人,TypeScript全无必要?
👉导读
前端框架的复杂度最近一段时间频频遭到质疑,引发了一些吐槽,甚至有一篇文章提到:『前端所有主流的框架,都是在自欺欺人』。本文主要是向前端的初学者介绍前端框架的发展历程及设计思想,比如为何要引入这样那样的“复杂度”?这样『设计』有什么好处?是为了解决什么问题?了解其背后的原因,我们或许就不会那么多抱怨了。👉目录
1 前端框架怎么你了?2 前端框架的发展史3 前端框架为何要引入“复杂度”4 为什么当下这么多主流框架?5 知其然知其所以然6 题外话:Typescript 引入复杂度了吗7 总结01
1.1 探秘 Svelte 的新特性 runes
<script> let count = 0; const handleClick = () => { count += 1; }</script><div> count: {count} <button on:click={handleClick}>inc</button></div>
1.2 外国小伙的槽点
HTML 不是前端框架最佳的选项; 前端框架引入了复杂度问题; 前端框架编造出的模板语法完全没必要,用 DOM API 更好; 不同框架的模板语法不统一。
02
03
3.1 HTML 模板:隐藏实现细节,降低开发难度
<div class="block">a block</div><button class="toggle-button">toggle block</button><script>const block = document.querySelector('.block');const toggleButton = document.querySelector('.toggle-button');let blockVisible = true;toggleButton.addEventListener('click', () => { blockVisible = !blockVisible; block.style.display = blockVisible ? 'block' : 'none'; });</script>
<template>
<div v-if="blockVisible">a block</div>
<button @click="handleClick">toggle block</button>
</template>
<script setup>
const blockVisible = ref(true);
const handleClick = () => {
blockVisible.value = !blockVisible.value;
}
</script>
3.2 组件:提升复用性
<div class="todo-list">
<div>
new item 1
<button>X</button>
</div>
<div>
new item 2
<button>X</button>
</div>
</div>
<button>add item</button>
<script>
const todoList = document.querySelector('.todo-list');
const addButton = document.querySelector('button');
addButton.addEventListener('click', () => {
const todoItem = document.createElement('div');
const deleteButton = document.createElement('button');
todoItem.appendChild(deleteButton);
todoItem.innerText = 'new item';
todoList.appendChild(todoItem);
});
</script>
<div class="todo-list">
</div>
<button>add item</button>
<script>
class TodoItem {
constructor(content) {
const todoItem = document.createElement('div');
const deleteButton = document.createElement('button');
todoItem.appendChild(deleteButton);
todoItem.innerText = content;
this.dom = todoItem;
}
appendTo(parent) {
return parent.appendChild(this.dom);
}
}
const todoList = document.querySelector('.todo-list');
const addButton = document.querySelector('button');
new TodoItem('new item 1').appendTo(todoList);
new TodoItem('new item 2').appendTo(todoList);
addButton.addEventListener('click', () => {
new TodoItem().appendTo(todoList);
});
</script>
<template>
<div>
new item
<button>delete</button>
</div>
</template>
<div class="todo-list">
<todo-item v-for="item in todoItems" />
</div>
<button @click="handleClick">add item</button>
<script setup>
import TodoItem from './todo-item.Vue';
const todoItems = [];
const handleClick = () => {
todoItems.push({});
};
</script>
3.3 VDOM/编译器机制:跨平台
interface IUIOperations {
createElement(type: ElementType): Element;
removeElement(ele: Element);
updateElement(ele: Element);
setProperty(ele: Element, propName: string; propValue: string);
removeProperty(...
}
class DOMUIOperations implements IUIOperations {
createElement(type: ElementType): Element {
return new DOMElement(document.createElement(type.getName()));
}
removeElement(ele: Element) {
...
}
updateElement(ele: Element)
...
}
class NativeUIOperations implements IUIOperations {
createElement(type: ElementType): Element {
const nativeHandle = xxos.createComponent(type.getName());
return new NativeElement(
nativeHandle
);
}
removeElement(ele: Element) {
...
}
updateElement(ele: Element)
...
}
3.4 数据响应式:降低数据管理复杂度
<div>
</div>
<button>reverse</button>
<script>
const div = document.querySelector('div');
const button = document.querySelector('button');
let msg = 'hello, world';
div.innerText = msg;
button.addEventListener('click', () => {
msg = msg.split('').reverse().join('');
div.innerText = msg;
});
</script>
<div>
{msg}
</div>
<button on:click="handleClick">reverse</button>
<script>
let msg = 'hello, world';
const handleClick = () => {
msg = msg.split('').reverse().join('');
};
</script>
04
4.1 React 诞生的意义
4.2 为何还有 Vue
4.3 Svelte
4.4 各有千秋
4.5 整体来看
05
06
07