【原生JS教程】第19课:工具库与框架集成

【原生JS教程】第19课:工具库与框架集成

👽第19节:工具库与框架集成

💻引言

在现代前端开发中,原生JavaScript虽然强大,但在实际项目中我们往往需要借助成熟的工具库和框架来提高开发效率和代码质量。jQuery、Vue.js和React是前端开发中最为流行的工具和框架,它们各自有不同的设计理念和使用场景。

本节将深入探讨这些工具和框架的基础知识,以及它们与原生JavaScript的集成方式,帮助你理解如何在项目中合理选择和使用这些技术。

💵一、jQuery 基础

🚀1.1 jQuery 简介

jQuery是一个快速、简洁的JavaScript库,它使得HTML文档遍历、事件处理、动画和Ajax交互更加简单。

// 原生JavaScript vs jQuery

// 原生JavaScript

document.addEventListener('DOMContentLoaded', function() {

const button = document.getElementById('myButton');

button.addEventListener('click', function() {

const list = document.getElementById('myList');

const items = list.querySelectorAll('li');

items.forEach(item => {

item.style.color = 'blue';

});

});

});

// jQuery

$(document).ready(function() {

$('#myButton').click(function() {

$('#myList li').css('color', 'blue');

});

});

⚡1.2 jQuery 核心概念

✅1.2.1 jQuery 对象与DOM对象

// DOM对象

const domElement = document.getElementById('myDiv');

domElement.style.color = 'red';

// jQuery对象

const $jqueryElement = $('#myDiv');

$jqueryElement.css('color', 'red');

// 转换

const domFromJquery = $jqueryElement[0]; // jQuery对象转DOM对象

const jqueryFromDom = $(domElement); // DOM对象转jQuery对象

✅1.2.2 选择器

// 基本选择器

$('#myId') // ID选择器

$('.myClass') // 类选择器

$('div') // 标签选择器

$('div.class') // 组合选择器

// 层级选择器

$('ul li') // 后代选择器

$('ul > li') // 子元素选择器

$('li + span') // 相邻兄弟选择器

$('li ~ span') // 一般兄弟选择器

// 属性选择器

$('input[type="text"]') // 属性选择器

$('input[name^="user"]') // 属性开头

$('input[name$="name"]') // 属性结尾

$('input[name*="user"]') // 属性包含

// 过滤选择器

$('li:first') // 第一个元素

$('li:last') // 最后一个元素

$('li:even') // 偶数索引元素

$('li:odd') // 奇数索引元素

$('li:eq(2)') // 索引为2的元素

$('li:gt(2)') // 索引大于2的元素

$('li:lt(2)') // 索引小于2的元素

🌋1.3 jQuery DOM 操作

✅1.3.1 内容操作

// 获取和设置HTML内容

$('#myDiv').html(); // 获取HTML内容

$('#myDiv').html('

新内容

'); // 设置HTML内容

// 获取和设置文本内容

$('#myDiv').text(); // 获取文本内容

$('#myDiv').text('新文本'); // 设置文本内容

// 获取和设置表单值

$('#myInput').val(); // 获取值

$('#myInput').val('新值'); // 设置值

// 属性操作

$('#myImg').attr('src'); // 获取属性

$('#myImg').attr('src', 'new.jpg'); // 设置属性

$('#myDiv').removeAttr('class'); // 移除属性

✅1.3.2 CSS 操作

// CSS类操作

$('#myDiv').addClass('newClass'); // 添加类

$('#myDiv').removeClass('oldClass'); // 移除类

$('#myDiv').toggleClass('active'); // 切换类

$('#myDiv').hasClass('someClass'); // 检查是否有类

// 直接样式操作

$('#myDiv').css('color', 'red'); // 设置单个样式

$('#myDiv').css({ // 设置多个样式

'color': 'red',

'font-size': '16px'

});

$('#myDiv').css('color'); // 获取样式值

✅1.3.3 元素操作

// 创建元素

const $newDiv = $('

新元素
');

// 插入元素

$('#container').append($newDiv); // 内部末尾插入

$('#container').prepend($newDiv); // 内部开头插入

$('#target').before($newDiv); // 外部前面插入

$('#target').after($newDiv); // 外部后面插入

// 包装元素

$('#mySpan').wrap('

'); // 包装元素

$('#myDiv').wrapInner(''); // 包装内容

// 删除元素

$('#myDiv').remove(); // 删除元素及其事件

$('#myDiv').detach(); // 删除元素但保留事件

$('#myDiv').empty(); // 清空内容

✅1.4 jQuery 事件处理

// 基本事件绑定

$('#myButton').click(function() {

console.log('按钮被点击');

});

$('#myInput').on('focus blur', function(event) {

console.log('事件类型:', event.type);

});

// 事件委托

$('#parent').on('click', '.child', function() {

console.log('子元素被点击');

});

// 自定义事件

$('#myDiv').on('customEvent', function(event, data) {

console.log('自定义事件触发:', data);

});

// 触发自定义事件

$('#myDiv').trigger('customEvent', ['额外数据']);

// 事件对象

$('#myButton').click(function(event) {

event.preventDefault(); // 阻止默认行为

event.stopPropagation(); // 阻止事件冒泡

console.log('事件目标:', event.target);

console.log('当前元素:', event.currentTarget);

});

✅1.5 jQuery 动画效果

// 基本动画

$('#myDiv').show(1000); // 显示动画

$('#myDiv').hide(1000); // 隐藏动画

$('#myDiv').toggle(1000); // 切换显示状态

// 淡入淡出

$('#myDiv').fadeIn(1000); // 淡入

$('#myDiv').fadeOut(1000); // 淡出

$('#myDiv').fadeToggle(1000); // 淡入淡出切换

// 滑动效果

$('#myDiv').slideDown(1000); // 滑下显示

$('#myDiv').slideUp(1000); // 滑上隐藏

$('#myDiv').slideToggle(1000); // 滑动切换

// 自定义动画

$('#myDiv').animate({

width: '200px',

height: '200px',

opacity: 0.5

}, 1000, 'swing', function() {

console.log('动画完成');

});

✅1.6 jQuery Ajax

// 基本Ajax请求

$.ajax({

url: '/api/data',

type: 'GET',

dataType: 'json',

success: function(data) {

console.log('请求成功:', data);

},

error: function(xhr, status, error) {

console.log('请求失败:', error);

}

});

// 快捷方法

$.get('/api/data', function(data) {

console.log(data);

});

$.post('/api/data', {name: 'John'}, function(data) {

console.log(data);

});

// 加载HTML内容

$('#content').load('/partials/content.html');

// 获取JSON数据

$.getJSON('/api/data.json', function(data) {

console.log(data);

});

🐱二、Vue.js 数据绑定

🚌2.1 Vue.js 简介

Vue.js是一个渐进式JavaScript框架,专注于构建用户界面。它采用自底向上的增量开发设计,核心库只关注视图层。

{{ message }}

🌏2 模板语法

✅2.2.1 插值

{{ message }}

{{ number + 1 }}

{{ ok ? 'YES' : 'NO' }}

{{ message.split('').reverse().join('') }}

✅2.2.2 指令

现在你看到我了

现在你看到我了

  • {{ item.text }}

  • {{ index }} - {{ item.text }}

  • {{ key }}: {{ value }}

✅2.3 事件处理

这个按钮被点击了 {{ counter }} 次。

✅2.4 表单输入绑定

Message is: {{ message }}

Textarea is: {{ textarea }}

选中的名字: {{ checkedNames }}

选中的: {{ picked }}

选中的: {{ selected }}

✅2.5 计算属性和侦听器

原始消息: "{{ message }}"

计算属性反转消息: "{{ reversedMessage }}"

方法反转消息: "{{ reversedMessageMethod() }}"

姓:

名:

全名: {{ fullName }}

问题:

{{ answer }}

🌐三、React 组件通信

✅3.1 React 简介

React是由Facebook开发的用于构建用户界面的JavaScript库。它采用组件化的开发模式,通过虚拟DOM提高性能。

// 基本React组件

class Hello extends React.Component {

render() {

return

Hello, {this.props.name}!

;

}

}

// 函数组件

function Welcome(props) {

return

Hello, {props.name}!

;

}

// 使用组件

ReactDOM.render(

,

document.getElementById('root')

);

✅3.2 组件状态和生命周期

class Clock extends React.Component {

constructor(props) {

super(props);

this.state = { date: new Date() };

}

componentDidMount() {

this.timerID = setInterval(

() => this.tick(),

1000

);

}

componentWillUnmount() {

clearInterval(this.timerID);

}

tick() {

this.setState({

date: new Date()

});

}

render() {

return (

Hello, world!

It is {this.state.date.toLocaleTimeString()}.

);

}

}

ReactDOM.render(

,

document.getElementById('root')

);

✅3.3 Props 和 State

3.3.1 Props(属性)

// 父组件向子组件传递数据

function UserCard(props) {

return (

{props.name}

{props.name}

{props.email}

);

}

class UserList extends React.Component {

constructor(props) {

super(props);

this.state = {

users: [

{ id: 1, name: 'Alice', email: 'alice@example.com', avatar: 'alice.jpg' },

{ id: 2, name: 'Bob', email: 'bob@example.com', avatar: 'bob.jpg' }

]

};

}

deleteUser = (userId) => {

this.setState({

users: this.state.users.filter(user => user.id !== userId)

});

}

render() {

return (

{this.state.users.map(user => (

key={user.id}

id={user.id}

name={user.name}

email={user.email}

avatar={user.avatar}

onDelete={this.deleteUser}

/>

))}

);

}

}

3.3.2 State(状态)

class Counter extends React.Component {

constructor(props) {

super(props);

this.state = {

count: 0,

step: 1

};

}

// 正确更新状态的方式

increment = () => {

this.setState((prevState) => ({

count: prevState.count + prevState.step

}));

}

decrement = () => {

this.setState((prevState) => ({

count: prevState.count - prevState.step

}));

}

// 批量更新状态

updateStep = (newStep) => {

this.setState({

step: newStep

});

}

// 异步状态更新

resetCounter = () => {

this.setState({ count: 0 }, () => {

console.log('计数器已重置,当前值:', this.state.count);

});

}

render() {

return (

计数器: {this.state.count}

步长: {this.state.step}

);

}

}

✅3.4 组件通信模式

3.4.1 父组件向子组件传递数据(Props Down)

// 父组件

class Parent extends React.Component {

constructor(props) {

super(props);

this.state = {

message: 'Hello from Parent',

userData: {

name: 'John',

age: 30

}

};

}

updateMessage = (newMessage) => {

this.setState({ message: newMessage });

}

render() {

return (

父组件

message={this.state.message}

userData={this.state.userData}

onUpdateMessage={this.updateMessage}

/>

);

}

}

// 子组件

class Child extends React.Component {

handleClick = () => {

this.props.onUpdateMessage('Message updated by Child');

}

render() {

return (

子组件

接收到的消息: {this.props.message}

用户姓名: {this.props.userData.name}

用户年龄: {this.props.userData.age}

);

}

}

3.4.2 子组件向父组件传递数据(Events Up)

// 子组件

class InputField extends React.Component {

constructor(props) {

super(props);

this.state = {

value: ''

};

}

handleChange = (event) => {

const newValue = event.target.value;

this.setState({ value: newValue });

// 通知父组件

if (this.props.onChange) {

this.props.onChange(newValue);

}

}

render() {

return (

type="text"

value={this.state.value}

onChange={this.handleChange}

placeholder={this.props.placeholder}

/>

);

}

}

// 父组件

class Form extends React.Component {

constructor(props) {

super(props);

this.state = {

inputValue: ''

};

}

handleInputChange = (value) => {

this.setState({ inputValue: value });

}

handleSubmit = () => {

console.log('提交的值:', this.state.inputValue);

}

render() {

return (

表单

placeholder="请输入内容"

onChange={this.handleInputChange}

/>

当前输入: {this.state.inputValue}

);

}

}

3.4.3 兄弟组件通信

// 父组件作为中介

class App extends React.Component {

constructor(props) {

super(props);

this.state = {

sharedData: '初始数据'

};

}

updateSharedData = (newData) => {

this.setState({ sharedData: newData });

}

render() {

return (

data={this.state.sharedData}

onUpdate={this.updateSharedData}

/>

data={this.state.sharedData}

/>

);

}

}

class SiblingA extends React.Component {

handleClick = () => {

this.props.onUpdate('来自SiblingA的数据');

}

render() {

return (

Sibling A

共享数据: {this.props.data}

);

}

}

class SiblingB extends React.Component {

render() {

return (

Sibling B

共享数据: {this.props.data}

);

}

}

3.5 Context API(跨层级组件通信)

// 创建Context

const ThemeContext = React.createContext('light');

const UserContext = React.createContext({

name: 'Guest',

signIn: () => {}

});

// 提供者组件

class App extends React.Component {

constructor(props) {

super(props);

this.state = {

theme: 'dark',

user: {

name: 'Alice',

signIn: this.signIn

}

};

}

signIn = (userName) => {

this.setState({

user: {

name: userName,

signIn: this.signIn

}

});

}

render() {

return (

);

}

}

// 中间组件(不需要传递props)

function Toolbar() {

return (

);

}

// 消费者组件

class ThemedButton extends React.Component {

render() {

return (

{theme => (

{user => (

)}

)}

);

}

}

🔨四、工具库与框架集成实践

✅4.1 jQuery 与现代框架集成

// 在React中使用jQuery插件

class DatePicker extends React.Component {

componentDidMount() {

// 初始化jQuery插件

$(this.datepickerRef).datepicker({

onSelect: (dateText) => {

this.props.onDateChange(dateText);

}

});

}

componentWillUnmount() {

// 清理jQuery插件

$(this.datepickerRef).datepicker('destroy');

}

render() {

return (

ref={ref => this.datepickerRef = ref}

type="text"

placeholder="选择日期"

/>

);

}

}

// 在Vue中使用jQuery

Vue.component('jquery-plugin', {

template: '

jQuery插件容器
',

mounted() {

// 初始化jQuery插件

$(this.$refs.container).someJQueryPlugin({

// 配置选项

});

},

beforeDestroy() {

// 清理jQuery插件

$(this.$refs.container).someJQueryPlugin('destroy');

}

});

✅4.2 状态管理集成

// Redux与React集成

import { createStore } from 'redux';

import { Provider, connect } from 'react-redux';

// Reducer

function counterReducer(state = { count: 0 }, action) {

switch (action.type) {

case 'INCREMENT':

return { count: state.count + 1 };

case 'DECREMENT':

return { count: state.count - 1 };

default:

return state;

}

}

// 创建store

const store = createStore(counterReducer);

// 连接组件

const mapStateToProps = (state) => ({

count: state.count

});

const mapDispatchToProps = (dispatch) => ({

increment: () => dispatch({ type: 'INCREMENT' }),

decrement: () => dispatch({ type: 'DECREMENT' })

});

class Counter extends React.Component {

render() {

return (

计数器: {this.props.count}

);

}

}

const ConnectedCounter = connect(

mapStateToProps,

mapDispatchToProps

)(Counter);

// 应用根组件

ReactDOM.render(

,

document.getElementById('root')

);

✅4.3 路由集成

// React Router

import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom';

function App() {

return (

);

}

function Home() {

return

首页

;

}

function About() {

return

关于

;

}

function Users() {

return

用户列表

;

}

❗五、重难点分析

✅5.1 jQuery 与原生JavaScript的性能对比

// 性能测试示例

console.time('原生JavaScript');

for (let i = 0; i < 10000; i++) {

document.querySelector('#myElement');

}

console.timeEnd('原生JavaScript');

console.time('jQuery');

for (let i = 0; i < 10000; i++) {

$('#myElement');

}

console.timeEnd('jQuery');

✅5.2 Vue.js 响应式原理

// 简化的响应式实现

class Reactive {

constructor(data) {

this.data = this.observe(data);

}

observe(data) {

if (!data || typeof data !== 'object') {

return data;

}

Object.keys(data).forEach(key => {

let value = data[key];

const self = this;

Object.defineProperty(data, key, {

enumerable: true,

configurable: true,

get() {

console.log(`获取 ${key}: ${value}`);

return value;

},

set(newVal) {

console.log(`设置 ${key}: ${newVal}`);

value = newVal;

self.notify();

}

});

// 递归处理嵌套对象

if (typeof value === 'object') {

this.observe(value);

}

});

return data;

}

notify() {

console.log('数据发生变化,需要更新视图');

}

}

// 使用示例

const reactiveData = new Reactive({

name: 'John',

age: 30

});

console.log(reactiveData.data.name); // 获取 name: John

reactiveData.data.name = 'Jane'; // 设置 name: Jane

✅5.3 React Hooks 与类组件的区别

// 类组件

class CounterClass extends React.Component {

constructor(props) {

super(props);

this.state = { count: 0 };

}

componentDidMount() {

document.title = `计数器: ${this.state.count}`;

}

componentDidUpdate() {

document.title = `计数器: ${this.state.count}`;

}

increment = () => {

this.setState({ count: this.state.count + 1 });

}

render() {

return (

计数: {this.state.count}

);

}

}

// 函数组件 + Hooks

function CounterHook() {

const [count, setCount] = useState(0);

useEffect(() => {

document.title = `计数器: ${count}`;

});

return (

计数: {count}

);

}

📚六、总结

本节介绍了三种主流的前端工具和框架:

jQuery:简化DOM操作和事件处理的经典库,适合快速开发和兼容老项目Vue.js:渐进式框架,学习曲线平缓,数据绑定和组件化特性强大React:组件化库,生态系统丰富,适合大型应用开发

每种工具都有其适用场景:

jQuery适合需要兼容老浏览器或快速原型开发的项目Vue.js适合中小型项目或需要快速上手的团队React适合大型复杂应用或需要高度定制的项目

在实际开发中,可以根据项目需求、团队技术栈和维护成本来选择合适的工具。

🧩七、高频面试题

✅1. jQuery的链式调用是如何实现的?

答案:

jQuery通过在每个方法中返回jQuery对象本身来实现链式调用。

// 简化实现

jQuery.fn = jQuery.prototype = {

init: function(selector) {

// 初始化逻辑

return this;

},

css: function(property, value) {

// 设置CSS逻辑

return this; // 返回this实现链式调用

},

addClass: function(className) {

// 添加类逻辑

return this;

},

on: function(event, handler) {

// 事件绑定逻辑

return this;

}

};

// 使用示例

$('#myDiv').css('color', 'red').addClass('highlight').on('click', handler);

✅2. Vue的双向数据绑定是如何实现的?

答案:

Vue 2.x使用Object.defineProperty()实现响应式,Vue 3.x使用Proxy实现。

// Vue 2.x 简化实现

function defineReactive(obj, key, val) {

Object.defineProperty(obj, key, {

enumerable: true,

configurable: true,

get() {

console.log(`读取属性 ${key}`);

return val;

},

set(newVal) {

if (newVal === val) return;

console.log(`设置属性 ${key} 为 ${newVal}`);

val = newVal;

// 触发视图更新

updateView();

}

});

}

// Vue 3.x 使用Proxy

function reactive(target) {

return new Proxy(target, {

get(target, key, receiver) {

console.log(`读取属性 ${key}`);

return Reflect.get(target, key, receiver);

},

set(target, key, value, receiver) {

console.log(`设置属性 ${key} 为 ${value}`);

const result = Reflect.set(target, key, value, receiver);

// 触发视图更新

updateView();

return result;

}

});

}

✅3. React中setState为什么是异步的?

答案:

setState设计为异步的主要原因是为了性能优化:

批量更新:将多次setState合并为一次更新避免不必要的重渲染:在事件处理中延迟执行保持内部一致性:确保组件状态的一致性

// setState异步示例

class Counter extends React.Component {

state = { count: 0 };

handleClick = () => {

// 这些调用会被批量处理

this.setState({ count: this.state.count + 1 });

this.setState({ count: this.state.count + 1 });

this.setState({ count: this.state.count + 1 });

// 最终count只增加1

// 使用函数形式可以正确更新

this.setState(prevState => ({ count: prevState.count + 1 }));

this.setState(prevState => ({ count: prevState.count + 1 }));

this.setState(prevState => ({ count: prevState.count + 1 }));

// 最终count增加3

};

}

✅4. Vue和React在组件通信上有何区别?

答案:

特性VueReact父子通信props/down, events/upprops/down, callbacks/up兄弟通信通过父组件中转或EventBus通过父组件中转或状态管理跨层级通信provide/inject, VuexContext API, Redux数据流双向绑定单向数据流// React Context vs Vue provide/inject

// React

const ThemeContext = React.createContext();

function App() {

return (

);

}

function ThemedButton() {

return (

{theme => }

);

}

// Vue

// 父组件

export default {

provide() {

return {

theme: 'dark'

};

}

};

// 子组件

export default {

inject: ['theme'],

template: ''

};

✅5. 如何在React中优化性能?

答案:

React性能优化的常见方法:

// 1. 使用React.memo优化函数组件

const MyComponent = React.memo(function({ data }) {

return

{data}
;

});

// 2. 使用useCallback避免函数重复创建

function Parent() {

const [count, setCount] = useState(0);

const handleClick = useCallback(() => {

console.log('点击');

}, []); // 依赖数组为空,函数不会重新创建

return ;

}

// 3. 使用useMemo缓存计算结果

function ExpensiveComponent({ list }) {

const expensiveValue = useMemo(() => {

return list.reduce((acc, item) => acc + item.value, 0);

}, [list]); // 只有list变化时才重新计算

return

{expensiveValue}
;

}

// 4. 使用PureComponent或shouldComponentUpdate

class OptimizedComponent extends React.PureComponent {

// PureComponent自动实现shouldComponentUpdate浅比较

render() {

return

{this.props.data}
;

}

}

// 5. 懒加载组件

const LazyComponent = React.lazy(() => import('./LazyComponent'));

function App() {

return (

加载中...

}>

);

}

✅6. Vue中的生命周期钩子有哪些?

答案:

Vue实例的生命周期钩子:

new Vue({

// 创建阶段

beforeCreate() {

// 实例创建前,数据观测和事件配置之前

},

created() {

// 实例创建后,已完成数据观测和属性、方法的运算

},

// 挂载阶段

beforeMount() {

// 挂载开始前,render函数首次被调用

},

mounted() {

// 实例挂载到DOM后调用

},

// 更新阶段

beforeUpdate() {

// 数据更新时调用,发生在虚拟DOM重新渲染和打补丁之前

},

updated() {

// 数据更改导致的虚拟DOM重新渲染和打补丁之后调用

},

// 销毁阶段

beforeDestroy() {

// 实例销毁之前调用

},

destroyed() {

// 实例销毁后调用

}

});

✅7. jQuery如何处理跨域请求?

答案:

jQuery提供多种处理跨域请求的方式:

// 1. JSONP(只支持GET请求)

$.ajax({

url: 'http://example.com/api/data',

type: 'GET',

dataType: 'jsonp',

jsonpCallback: 'callback',

success: function(data) {

console.log(data);

}

});

// 2. CORS(需要服务器支持)

$.ajax({

url: 'http://api.example.com/data',

type: 'POST',

data: { name: 'John' },

xhrFields: {

withCredentials: true // 发送凭据

},

success: function(data) {

console.log(data);

}

});

// 3. 代理服务器

$.ajax({

url: '/proxy/api/data', // 通过同源代理访问

type: 'POST',

data: { name: 'John' },

success: function(data) {

console.log(data);

}

});

✅8. React Hooks相比类组件有哪些优势?

答案:

// 1. 更简洁的代码

// 类组件

class Counter extends React.Component {

constructor(props) {

super(props);

this.state = { count: 0 };

}

increment = () => {

this.setState({ count: this.state.count + 1 });

}

render() {

return (

计数: {this.state.count}

);

}

}

// Hooks

function Counter() {

const [count, setCount] = useState(0);

return (

计数: {count}

);

}

// 2. 更好的逻辑复用

// 自定义Hook

function useLocalStorage(key, initialValue) {

const [storedValue, setStoredValue] = useState(() => {

try {

const item = window.localStorage.getItem(key);

return item ? JSON.parse(item) : initialValue;

} catch (error) {

return initialValue;

}

});

const setValue = (value) => {

try {

setStoredValue(value);

window.localStorage.setItem(key, JSON.stringify(value));

} catch (error) {

console.error(error);

}

};

return [storedValue, setValue];

}

// 使用自定义Hook

function MyComponent() {

const [name, setName] = useLocalStorage('name', 'John');

return (

value={name}

onChange={(e) => setName(e.target.value)}

/>

);

}

✅9. Vue中的computed和watch有什么区别?

答案:

new Vue({

data: {

firstName: 'John',

lastName: 'Doe',

question: ''

},

computed: {

// 计算属性:基于响应式依赖进行缓存

fullName() {

console.log('计算fullName'); // 只有依赖变化时才执行

return this.firstName + ' ' + this.lastName;

},

// 计算属性的getter和setter

fullNameWithSetter: {

get() {

return this.firstName + ' ' + this.lastName;

},

set(newValue) {

const names = newValue.split(' ');

this.firstName = names[0];

this.lastName = names[names.length - 1];

}

}

},

watch: {

// 侦听器:观察和响应数据变动

question(newVal, oldVal) {

console.log('question变化了');

// 执行异步操作或开销较大的操作

this.getAnswer();

},

// 深度侦听

someObject: {

handler(newVal, oldVal) {

console.log('对象变化了');

},

deep: true // 深度侦听

},

// 立即执行

immediateValue: {

handler(newVal) {

console.log('立即执行');

},

immediate: true // 创建时立即执行

}

},

methods: {

getAnswer() {

// 模拟异步操作

}

}

});

✅10. 如何在项目中选择合适的前端框架?

答案:

选择前端框架需要考虑以下因素:

// 评估标准

const frameworkSelectionCriteria = {

// 项目规模

projectSize: {

small: 'jQuery, Vue', // 小型项目

medium: 'Vue, React', // 中型项目

large: 'React, Angular' // 大型项目

},

// 团队技能

teamSkills: {

beginners: 'Vue', // 学习曲线平缓

experienced: 'React', // 生态丰富

enterprise: 'Angular' // 企业级解决方案

},

// 性能要求

performance: {

high: 'React, Vue 3', // 虚拟DOM, Composition API

medium: 'Vue 2', // 响应式系统

low: 'jQuery' // 简单DOM操作

},

// 开发速度

developmentSpeed: {

fast: 'Vue', // 开发效率高

balanced: 'React', // 生态丰富

structured: 'Angular' // 强类型约束

},

// 社区支持

community: {

large: 'React', // 最大社区

growing: 'Vue', // 快速增长

stable: 'jQuery, Angular' // 成熟稳定

}

};

// 决策矩阵

function chooseFramework(project) {

const scores = {

jQuery: 0,

Vue: 0,

React: 0,

Angular: 0

};

// 根据项目需求评分

// ... 评分逻辑

return Object.keys(scores).reduce((a, b) =>

scores[a] > scores[b] ? a : b

);

}

📘这些面试题涵盖了工具库和框架的核心概念和实际应用场景,掌握这些知识能够帮助你在实际项目中做出更好的技术选择。

相关推荐

【求助】骨组织4%多聚甲醛固定后暂时不切片如何保存
2025男裤十大品牌排行榜
365在线体育官方网站入口

2025男裤十大品牌排行榜

📅 07-19 👁️ 5248
烟雨江湖蓝鲸屿怎么去 蓝鲸屿在哪
36578130

烟雨江湖蓝鲸屿怎么去 蓝鲸屿在哪

📅 07-03 👁️ 5799