Basic understanding of Behaviors

behaviors

Behaviors are features used for code sharing between components, similar to "mixins" or "traits" in some programming languages.
Each behavior can contain a set of properties, data, lifecycle functions and methods. When a component references it, its properties, data, and methods will be merged into the component, and the lifecycle functions will also be called at the corresponding time. Each component can reference multiple behaviors, and behaviors can also reference other behaviors.

Override and combination rules for fields with the same name

Components and the behaviors they reference can contain fields with the same name. These fields are processed as follows:

If there are properties or methods with the same name:

1、若组件本身有这个属性或方法,则组件的属性或方法会覆盖 behavior 中的同名属性或方法;
2、若组件本身无这个属性或方法,则在组件的 behaviors 字段中定义靠后的 behavior 的属性或方法会覆盖靠前的同名属性或方法;
3、在 2 的基础上,若存在嵌套引用 behavior 的情况,则规则为:引用者 behavior 覆盖 被引用的 behavior 中的同名属性或方法。

If there is a data field with the same name:

1、若同名的数据字段都是对象类型,会进行对象合并;
2、其余情况会进行数据覆盖,覆盖规则为: 引用者 behavior > 被引用的 behavior 、 靠后的 behavior > 靠前的 behavior。(优先级高的覆盖优先级低的,最大的为优先级最高)

Lifecycle functions and observers will not cover each other, but will be called one by one at the corresponding trigger timing:

1、对于不同的生命周期函数之间,遵循组件生命周期函数的执行顺序;
2、对于同种生命周期函数和同字段 observers ,遵循如下规则:
	1、behavior 优先于组件执行;
	2、被引用的 behavior 优先于 引用者 behavior 执行;
	3、靠前的 behavior 优先于 靠后的 behavior 执行;
3、如果同一个 behavior 被一个组件多次引用,它定义的生命周期函数和 observers 不会重复执行。

Guess you like

Origin blog.csdn.net/qq_52654932/article/details/131196667