---
title: Behavior
summary: "介绍 Behavior() 构造器的配置参数，用于自定义组件间的代码共享。"
questions:
  - Behavior 构造器接受哪些配置参数？
  - Behavior 中的 lifetimes 字段支持哪些生命周期函数？
  - 如何在 Behavior 中定义属性和方法？
  - Behavior 可以引用其他 Behavior 吗？
  - 如何注册并导出一个 Behavior？
---

# Behavior(config: Object)

注册一个 `behavior`，接受一个 `Object` 类型的参数。

## 参数

| 属性       | 类型         | 必填 | 描述                                                                                                                                          | 最低版本 |
| ---------- | ------------ | ---- | --------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
| properties | Object Map   | 否   | 同组件的属性                                                                                                                                  |          |
| data       | Object       | 否   | 同组件的数据                                                                                                                                  |          |
| methods    | Object       | 否   | 同自定义组件的方法                                                                                                                            |          |
| behaviors  | String Array | 否   | 引入其它的 behavior                                                                                                                           |          |
| lifetimes  | Object       | 否   | 组件生命周期声明对象，包括 created, attached, detached, 参见 [组件生命周期](/cn/miniapp/develop/miniapp/framework/custom-component/lifetimes) |          |
| created    | Function     | 否   | 生命周期函数，在 lifetimes 字段内进行声明                                                                                                     |          |
| attached   | Function     | 否   | 生命周期函数，在 lifetimes 字段内进行声明                                                                                                     |          |
| detached   | Function     | 否   | 生命周期函数，在 lifetimes 字段内进行声明                                                                                                     |          |

## 示例代码

```js
// my-behavior.js
export default Behavior({
  behaviors: [],
  properties: {
    myBehaviorProperty: {
      type: String,
    },
  },
  data: {
    myBehaviorData: {},
  },
  lifetimes: {
    attached: function () {},
  },
  methods: {
    myBehaviorMethod: function () {},
  },
});
```
