---
title: 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 a `behavior`, the `behavior` properties, data, and methods will be merged into the component. The lifecycle functions will be called at an appropriate time point. Each component can reference multiple `behaviors`, and a `behavior` can also reference another `behavior`.

For more information about parameter meanings and usage, see [Behavior Documentation](/en/miniapp/develop/miniapp/framework/api/behavior).

## Usage in a component

For reference by components, behaviors can be listed one by one in the `behaviors` definition field.

**Sample code:**

```js
// my-component.js
import myBehavior from 'my-behavior';
Component({
  behaviors: [myBehavior],
  properties: {
    myProperty: {
      type: String
    }
  },
  data: {
    myData: 'my-component-data'
  },
  lifetimes: {
    created: function () {
    console.log('[my-component] created')
    },
    attached: function () {
      console.log('[my-component] attached')
    },
  }
  methods: {
    myMethod: function () {
      console.log('[my-component] log by myMethod')
    },
  }
})
```

In the above example, `my-behavior` is added to the definition of `my-component`. The `my-behavior` structure looks like this:

- Property: `myBehaviorProperty`
- Data field: `myBehaviorData`
- Method: `myBehaviorMethod`
- Lifecycle functions: `attached` and `created`

As a result, the final structure of `my-component` is:

- Property: `myBehaviorProperty` and `myProperty`
- Data field: `myBehaviorData` and `myData`
- Method: `myBehaviorMethod` and `myMethod`
- Lifecycle functions: `attached` and `created`

When a component triggers the lifecycle functions, the lifecycle functions in the above example are executed in the following sequence:

1. [my-behavior] created
2. [my-component] created
3. [my-behavior] attached
4. [my-component] attached

For more information about rules, see **Overwriting and combination rules for fields with the same name**.

### Overwriting and combination rules for fields with the same name

A component and the `behavior` it references can contain fields with the same name. These fields are handled as follows:

- For properties or methods with the same name:

  - If the component itself has this property or method, the component's property or method will overwrite the property or method with the same name in `behavior`.
  - If the component itself does not have this property or method, the `behavior` property or method last defined in the `behaviors` field of the component will overwrite the earlier property or method with the same name.
  - Based on the previous rule, if there is a nested reference to `behavior`, the rule is that the parent `behavior` overwrites the property or method with the same name in the child `behavior`.

- For data fields (`data`) with the same name:

  - If data fields with the same name are all object types, the objects will be merged.
  - In other cases, a former field overwrites data in a latter field when they are sorted in the following descending order of priority: component > parent `behavior` > child `behavior`, later `behavior` > earlier `behavior`. The fields with high priority overwrites the data in the fields with low priority. The `behavior` that is last defined has the highest priority among all `behavior` definitions.

- Lifecycle functions do not overwrite each other, but are called one by one at the specified trigger timing:
  - For different types of lifecycle functions, follow the execution sequence of component lifecycle functions.
  - For the same type of lifecycle functions, follow the rules below:
    - A `behavior` takes precedence over a component.
    - A child `behavior` takes precedence over the parent `behavior`.
    - The `behavior` that appears earlier takes precedence over those that appear later.
  - If the same `behavior` is referenced multiple times by a component, the lifecycle functions it defines will be executed only once.
