2024年最新Vue中的虚拟dom,2024年最新Web前端开发实习面试题

总结

根据路线图上的重点去进行有针对性的学习,在学习过程中,学会写笔记,做总结。

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

这里分享一些前端学习笔记:

  • html5 / css3 学习笔记

  • JavaScript 学习笔记

  • Vue 学习笔记

/当前节点的标签名/

this.tag = tag

/当前节点对应的对象,包含了具体的一些数据信息,是一个VNodeData类型,可以参考VNodeData类型中的数据信息/

this.data = data

/当前节点的子节点,是一个数组/

this.children = children

/当前节点的文本/

this.text = text

/当前虚拟节点对应的真实dom节点/

this.elm = elm

/当前节点的名字空间/

this.ns = undefined

/编译作用域/

this.context = context

/函数化组件作用域/

this.functionalContext = undefined

/节点的key属性,被当作节点的标志,用以优化/

this.key = data && data.key

/组件的option选项/

this.componentOptions = componentOptions

/当前节点对应的组件的实例/

this.componentInstance = undefined

/当前节点的父节点/

this.parent = undefined

/简而言之就是是否为原生HTML或只是普通文本,innerHTML的时候为true,textContent的时候为false/

this.raw = false

/静态节点标志/

this.isStatic = false

/是否作为跟节点插入/

this.isRootInsert = true

/是否为注释节点/

this.isComment = false

/是否为克隆节点/

this.isCloned = false

/是否有v-once指令/

this.isOnce = false

}

// DEPRECATED: alias for componentInstance for backwards compat.

/* istanbul ignore next https://github.com/answershuto/learnVue*/

get child (): Component | void {

return this.componentInstance

}

}

这里对VNode进行稍微的说明:

  • 所有对象的 context 选项都指向了 Vue 实例

  • elm 属性则指向了其相对应的真实 DOM 节点

vue是通过createElement生成VNode

源码create-element.js

export function createElement (

context: Component,

tag: any,

data: any,

children: any,

normalizationType: any,

alwaysNormalize: boolean

): VNode | Array {

if (Array.isArray(data) || isPrimitive(data)) {

normalizationType = children

children = data

data = undefined

}

if (isTrue(alwaysNormalize)) {

normalizationType = ALWAYS_NORMALIZE

}

return _createElement(context, tag, data, children, normalizationType)

}

上面可以看到createElement 方法实际上是对 _createElement 方法的封装,对参数的传入进行了判断

export function _createElement(

context: Component,

tag?: string | Class | Function | Object,

data?: VNodeData,

children?: any,

normalizationType?: number

): VNode | Array {

if (isDef(data) && isDef((data: any).ob)) {

process.env.NODE_ENV !== ‘production’ && warn(

Avoid using observed data object as vnode data: ${JSON.stringify(data)}\n +

‘Always create fresh vnode data objects in each render!’,

context`

)

return createEmptyVNode()

}

// object syntax in v-bind

if (isDef(data) && isDef(data.is)) {

tag = data.is

}

if (!tag) {

// in case of component :is set to falsy value

return createEmptyVNode()

}

// support single function children as default scoped slot

if (Array.isArray(children) &&

typeof children[0] === ‘function’

) {

data = data || {}

data.scopedSlots = { default: children[0] }

children.length = 0

}

if (normalizationType === ALWAYS_NORMALIZE) {

children = normalizeChildren(children)

} else if ( === SIMPLE_NORMALIZE) {

children = simpleNormalizeChildren(children)

}

// 创建VNode

}

可以看到_createElement接收5个参数:

  • context 表示 VNode 的上下文环境,是 Component 类型

  • tag 表示标签,它可以是一个字符串,也可以是一个 Component

  • data 表示 VNode 的数据,它是一个 VNodeData 类型

  • children 表示当前 VNode的子节点,它是任意类型的

  • normalizationType 表示子节点规范的类型,类型不同规范的方法也就不一样,主要是参考 render 函数是编译生成的还是用户手写的

根据normalizationType 的类型,children会有不同的定义

if (normalizationType === ALWAYS_NORMALIZE) {

children = normalizeChildren(children)

} else if ( === SIMPLE_NORMALIZE) {

children = simpleNormalizeChildren(children)

}

simpleNormalizeChildren方法调用场景是 render 函数是编译生成的

normalizeChildren方法调用场景分为下面两种:

render 函数是用户手写的

编译 slot、v-for 的时候会产生嵌套数组

无论是simpleNormalizeChildren还是normalizeChildren都是对children进行规范(使children 变成了一个类型为 VNode 的 Array),这里就不展开说了

规范化children的源码位置在:src/core/vdom/helpers/normalzie-children.js

在规范化children后,就去创建VNode

let vnode, ns

// 对tag进行判断

if (typeof tag === ‘string’) {

let Ctor

ns = (context.KaTeX parse error: Expected 'EOF', got '&' at position 7: vnode &̲& context.vnode.ns) || config.getTagNamespace(tag)

if (config.isReservedTag(tag)) {

// 如果是内置的节点,则直接创建一个普通VNode

vnode = new VNode(

config.parsePlatformTagName(tag), data, children,

undefined, undefined, context

)

} else if (isDef(Ctor = resolveAsset(context.$options, ‘components’, tag))) {

// component

// 如果是component类型,则会通过createComponent创建VNode节点

vnode = createComponent(Ctor, data, context, children, tag)

} else {

vnode = new VNode(

tag, data, children,

undefined, undefined, context

)

}

} else {

// direct component options / constructor

vnode = createComponent(tag, data, context, children)

}

createComponent同样是创建VNode

源码位置:src/core/vdom/create-component.js

export function createComponent (

Ctor: Class | Function | Object | void,

data: ?VNodeData,

context: Component,

children: ?Array,

tag?: string

): VNode | Array | void {

if (isUndef(Ctor)) {

return

}

// 构建子类构造函数

const baseCtor = context.$options._base

// plain options object: turn it into a constructor

if (isObject(Ctor)) {

Ctor = baseCtor.extend(Ctor)

}

// if at this stage it’s not a constructor or an async component factory,

// reject.

if (typeof Ctor !== ‘function’) {

if (process.env.NODE_ENV !== ‘production’) {

warn(Invalid Component definition: ${String(Ctor)}, context)

}

return

}

// async component

let asyncFactory

if (isUndef(Ctor.cid)) {

asyncFactory = Ctor

Ctor = resolveAsyncComponent(asyncFactory, baseCtor, context)

if (Ctor === undefined) {

return createAsyncPlaceholder(

asyncFactory,

data,

context,

children,

tag

)

}

}

data = data || {}

// resolve constructor options in case global mixins are applied after

// component constructor creation

resolveConstructorOptions(Ctor)

最后

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

大厂面试问深度,小厂面试问广度,如果有同学想进大厂深造一定要有一个方向精通的惊艳到面试官,还要平时遇到问题后思考一下问题的本质,找方法解决是一个方面,看到问题本质是另一个方面。还有大家一定要有目标,我在很久之前就想着以后一定要去大厂,然后默默努力,每天看一些大佬们的文章,总是觉得只有再学深入一点才有机会,所以才有恒心一直学下去。

  • 16
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值