> ## Documentation Index
> Fetch the complete documentation index at: https://docs.projectreal.gg/llms.txt
> Use this file to discover all available pages before exploring further.

# hookmetamethod

> 在 value 上查找 metamethod function，并通过 hookfunction hook 该 function。first argument 可以是 raw metafield lookup 接受的任何 value。second argument 必须是 string metamethod name，third argument 必须是 function replacement。如果 raw metafield lookup 找不到 named metafield，该 function 会对 argument 2 抛出 "Metamethod doesn't exist"。如果 metafield 存在但不是 function，会对 argument 2 抛出 "Metamethod isn't a function"。当 metafield 是 function 时，hookmetamethod 会以 found metamethod function 作为 target 调用 hookfunction，并返回 hookfunction 的 cloned original closure。

<div className="real-function-page">
  ```lua theme={null}
  hookmetamethod(object: any, metamethod: string, replacement: function): function
  ```

  ## 参数

  <ParamField path={"object"} type={"any"} required>
    其 methods 由 raw metafield lookup 检查的 value。
  </ParamField>

  <ParamField path={"metamethod"} type={"string"} required>
    传给 raw metafield lookup 的 Metafield name。
  </ParamField>

  <ParamField path={"replacement"} type={"function"} required>
    作为 found metamethod function 的 replacement 传给 hookfunction 的 Function。
  </ParamField>

  ## 返回值

  <ResponseField name={"original"} type={"function"}>
    hookfunction 返回的 cloned original metamethod closure。
  </ResponseField>

  ## 示例

  Hook an existing metamethod function.

  ```lua theme={null}
  local object = setmetatable({}, {
      __index = function(_, key)
          return key
      end,
  })

  local original
  original = hookmetamethod(object, "__index", function(self, key)
      if key == "Name" then
          return "Real"
      end

      return original(self, key)
  end)
  ```
</div>
