forOwn

说明

遍历对象自身的可枚举属性, 不包括继承而来的属性

源码

用法

forOwn.test.ts
1
import { forOwn } from "../../utility/object/forOwn";
2
3
describe('forOwn tests...', () => {
4
test('method forOwn should traversal the object with its own property', () => {
5
const received = {
6
name: 'duanzhaoyang',
7
age: 21,
8
address: 'Dongguan',
9
};
10
const expected = {
11
name: 'duanzhaoyang',
12
age: 21,
13
address: 'Dongguan',
14
};
15
16
forOwn<typeof received>(received, (key, value, origin) => {
17
expect(expected[key as keyof typeof expected]).toBe(value);
18
expect(origin).toBe(received);
19
});
20
});
21
22
test('method forOwn should traversal the object without the property which were inherite by prototype', () => {
23
class Received {
24
public name: string = '';
25
public age: number = 0;
26
}
27
Received.prototype.isCorrect = false;
28
const expected = {
29
name: '',
30
age: 0,
31
};
32
33
const received = new Received();
34
35
forOwn(received, (key, value, origin) => {
36
expect(expected[key as keyof typeof expected]).toBe(value);
37
expect(key !== 'isCorrect').toBeTruthy();
38
expect(origin).toBe(received);
39
});
40
});
41
});
Copied!
最近更新 1yr ago
复制链接