---
title: Component Constructor
---

The `Component` constructor is used to define a component. You can specify the properties, data, and methods of the component when calling the `Component` constructor.

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

```js
Component({
  behaviors: [],

  properties: {
    myProperty: {
      // Property name
      type: String,
      value: '',
    },
    myProperty2: String, // Simplified definition
  },

  data: {}, // Private data, used for template rendering

  lifetimes: {
    created: function () {},
    attached: function () {},
    detached: function () {},
  },

  pageLifetimes: {
    // Lifecycle function of the page where the component is located
    show: function () {},
    hide: function () {},
  },

  methods: {
    onMyButtonTap: function () {
      this.setData({
        // The method for updating properties and data is similar to the method for updating page data
      });
    },
    // Internal methods can start with an underscore
    _myPrivateMethod: function () {
      // Set data.A[0].B to 'myPrivateData'
      this.setData({
        'A[0].B': 'myPrivateData',
      });
    },
    _propertyChange: function (newVal, oldVal) {},
  },
});
```
