Skip to content

Function: constantize()

ts
function constantize<T>(obj): void;

Freezes an object and its immediate properties (shallow freeze). 冻结对象及其直接属性(浅冻结)。

Type Parameters

Type Parameter
T extends Record<string, any>

Parameters

ParameterTypeDescription
objTThe object to freeze / 要冻结的对象

Returns

void

Example

ts
const config = { api: { url: 'https://api.example.com', timeout: 5000 }, debug: true }
constantize(config)
Object.isFrozen(config) // => true
Object.isFrozen(config.api) // => true
try {
  config.debug = false
} catch (e) {
  e.message // => Cannot assign to read only property 'debug'...
}

Since

1.0.0

Released under the MIT License.