Object
Object 是 ES5 中的对象,操作对象的构造函数
API
| name | 说明 |
|---|---|
prototype.__proto__ |
。 |
prototype.constructor |
|
assign() |
给对象添加一个属性并指定该属性的配置。 |
create() |
使用指定的原型对象和属性创建一个新对象。 |
defineProperties() |
给对象添加一个属性并指定该属性的配置。 |
defineProperty() |
给对象添加多个属性并分别指定它们的配置。 |
entries() |
返回给定对象自身可枚举属性的 [key, value] 数组。 |
freeze() |
冻结对象:其他代码不能删除或更改任何属性。 |
fromEntries() |
把键值对列表转换为一个对象。 |
getOwnPropertyDescriptor() |
返回对象指定的属性配置。 |
getOwnPropertyDescriptors() |
用来获取一个对象的所有自身属性的描述符。 |
getOwnPropertyNames() |
返回一个数组,它包含了指定对象所有的可枚举或不可枚举的属性名。 |
getOwnPropertySymbols() |
返回一个数组,它包含了指定对象自身所有的符号属性。 |
getPrototypeOf() |
返回指定对象的原型对象 |
is() |
比较两个值是否相同。所有 NaN 值都相等(这与==和===不同)。 |
isExtensible() |
判断对象是否可扩展。 |
isFrozen() |
判断对象是否已经冻结。 |
isSealed() |
判断对象是否已经密封。 |
keys() |
返回一个包含所有给定对象自身可枚举属性名称的数组。 |
preventExtensions() |
防止对象的任何扩展。 |
prototype.hasOwnProperty() |
返回一个布尔值,指示对象自身属性中是否具有指定的属性(也就是,是否有指定的键)。 |
prototype.isPrototypeOf() |
测试一个对象是否存在于另一个对象的原型链上。 |
prototype.propertyIsEnumerable() |
返回一个布尔值,表示指定的属性是否可枚举。 |
prototype.toLocaleString() |
返回一个该对象的字符串表示。此方法被用于派生对象为了特定语言环境的目的(locale-specific purposes)而重载使用。 |
prototype.toString() |
返回一个表示该对象的字符串。 |
prototype.valueOf() |
返回指定对象的原始值。 |
seal() |
防止其他代码删除对象的属性。 |
setPrototypeOf() |
设置对象的原型(即内部 [[Prototype]] 属性)。 |
values() |
返回给定对象自身可枚举值的数组。 |
Reflect
Reflect 是 ES6+ 中的对象,使用此方法操作对象。
特点
- 静态的,不能实例化,使用的方式类似于:
Math对象 - 将命令时的删除对象属性
delete,改为函数式Reflect.deleteProperty()
静态API
| name | 说明 |
|---|---|
apply() |
对一个函数进行调用操作,同时可以传入一个数组作为调用参数。和 Function.prototype.apply()功能类似。 |
construct() |
对构造函数进行 new 操作,相当于执行 new target(...args)。 |
defineProperty() |
和 Object.defineProperty() 类似。 |
deleteProperty() |
作为函数的delete操作符,相当于执行 delete target[name]。 |
get() |
获取对象身上某个属性的值,类似于 target[name]。 |
getOwnPropertyDescriptor() |
类似于 Object.getOwnPropertyDescriptor()。 |
getPrototypeOf() |
类似于 Object.getPrototypeOf()。 |
has() |
判断一个对象是否存在某个属性,和 in 运算符 的功能完全相同。 |
isExtensible() |
类似于 Object.isExtensible() |
ownKeys() |
返回一个包含所有自身属性(不包含继承属性)的数组。(类似于 Object.keys(), 但不会受enumerable影响)。 |
preventExtensions() |
类似于Object.preventExtensions()。返回一个Boolean。 |
set() |
将值分配给属性的函数。返回一个Boolean,如果更新成功,则返回true。 |
setPrototypeOf() |
类似于 Object.setPrototypeOf()。 |
Reflect Meta
- ECMAScript的元数据反射API的原型 reflect-meta
API
- defineMetadata
- hasMetadata
- hasOwnMetadata
- getMetadata
- getOwnMetadata
- getMetadataKeys
- deleteMetadata
// define metadata on an object or property
Reflect.defineMetadata(metadataKey, metadataValue, target);
Reflect.defineMetadata(metadataKey, metadataValue, target, propertyKey);
// check for presence of a metadata key on the prototype chain of an object or property
let result = Reflect.hasMetadata(metadataKey, target);
let result = Reflect.hasMetadata(metadataKey, target, propertyKey);
// check for presence of an own metadata key of an object or property
let result = Reflect.hasOwnMetadata(metadataKey, target);
let result = Reflect.hasOwnMetadata(metadataKey, target, propertyKey);
// get metadata value of a metadata key on the prototype chain of an object or property
let result = Reflect.getMetadata(metadataKey, target);
let result = Reflect.getMetadata(metadataKey, target, propertyKey);
// get metadata value of an own metadata key of an object or property
let result = Reflect.getOwnMetadata(metadataKey, target);
let result = Reflect.getOwnMetadata(metadataKey, target, propertyKey);
// get all metadata keys on the prototype chain of an object or property
let result = Reflect.getMetadataKeys(target);
let result = Reflect.getMetadataKeys(target, propertyKey);
// get all own metadata keys of an object or property
let result = Reflect.getOwnMetadataKeys(target);
let result = Reflect.getOwnMetadataKeys(target, propertyKey);
// delete metadata from an object or property
let result = Reflect.deleteMetadata(metadataKey, target);
let result = Reflect.deleteMetadata(metadataKey, target, propertyKey);
// apply metadata via a decorator to a constructor
@Reflect.metadata(metadataKey, metadataValue)
class C {
// apply metadata via a decorator to a method (property)
@Reflect.metadata(metadataKey, metadataValue)
method() {
}
}
// Design-time type annotations
function Type(type) { return Reflect.metadata("design:type", type); }
function ParamTypes(...types) { return Reflect.metadata("design:paramtypes", types); }
function ReturnType(type) { return Reflect.metadata("design:returntype", type); }
// Decorator application
@ParamTypes(String, Number)
class C {
constructor(text, i) {
}
@Type(String)
get name() { return "text"; }
@Type(Function)
@ParamTypes(Number, Number)
@ReturnType(Number)
add(x, y) {
return x + y;
}
}
// Metadata introspection
let obj = new C("a", 1);
let paramTypes = Reflect.getMetadata("design:paramtypes", inst, "add"); // [Number, Number]