从对象(日期对象)解构一个函数
Destructuring a function from object ( Date Object )
如果我想破坏我会做的对象:
const obj = {
a: 'a',
fn: () => 'some function'
}
// const fn = obj.fn;
// OR
const {
a,
fn
} = obj;
console.log( fn() );
这不适用于 Date
对象:
Uncaught TypeError: this is not a Date object.
const date = new Date();
const day = date.getDate();
console.log(day); // works
const {
getDate
} = date;
console.log( getDate() ); // doesn't work
为什么第一个对象可以做到这一点而不是 Date
?如果可能的话,人们将如何实现这一目标。
因为this
它不是日期对象。当您调用 getDate()
时没有其正确的上下文(即 date.getDate()
),那么您是在 window
(或严格模式下的 null
)的上下文中调用它。 window
和 null
都不是 Date 对象,因此函数失败。
尝试const getDate = date.getDate.bind(date);
演示:
const test = { fn : function() { return this.constructor; } };
const normal = test.fn();
console.log(normal); // object
const {fn} = test;
console.log( fn() ); // window
const bound = test.fn.bind(test);
console.log( bound() ); // object
这可能不值得,但您可以编写一个函数来帮助您从对象中解构方法。这里 bindMethods
使用助手 allKeys
执行此操作,它从对象的整个原型链中收集键,而后者又依赖于 walkPrototypeChain
。如果需要,它们显然可以折叠成一个函数。
const walkPrototypeChain = (process, init, finish) => (obj) => {
let currObj = obj, currRes = init();
do {
currRes = process(currRes, currObj)
} while (currObj = Object.getPrototypeOf(currObj))
return finish(currRes)
}
const allKeys = walkPrototypeChain(
(set, obj) => {Object.getOwnPropertyNames(obj).forEach(k => set.add(k)); return set},
() => new Set(),
s => [...s]
)
const bindMethods = (obj) => allKeys(obj).reduce(
(o, n) => typeof obj[n] == 'function' ? ({...o, [n]: obj[n].bind(obj)}) : o,
{}
)
const date = new Date()
const {getDate, getFullYear} = bindMethods(date) // or any other date function
console.log(getDate())
console.log(getFullYear())
如果我想破坏我会做的对象:
const obj = {
a: 'a',
fn: () => 'some function'
}
// const fn = obj.fn;
// OR
const {
a,
fn
} = obj;
console.log( fn() );
这不适用于 Date
对象:
Uncaught TypeError: this is not a Date object.
const date = new Date();
const day = date.getDate();
console.log(day); // works
const {
getDate
} = date;
console.log( getDate() ); // doesn't work
为什么第一个对象可以做到这一点而不是 Date
?如果可能的话,人们将如何实现这一目标。
因为this
它不是日期对象。当您调用 getDate()
时没有其正确的上下文(即 date.getDate()
),那么您是在 window
(或严格模式下的 null
)的上下文中调用它。 window
和 null
都不是 Date 对象,因此函数失败。
尝试const getDate = date.getDate.bind(date);
演示:
const test = { fn : function() { return this.constructor; } };
const normal = test.fn();
console.log(normal); // object
const {fn} = test;
console.log( fn() ); // window
const bound = test.fn.bind(test);
console.log( bound() ); // object
这可能不值得,但您可以编写一个函数来帮助您从对象中解构方法。这里 bindMethods
使用助手 allKeys
执行此操作,它从对象的整个原型链中收集键,而后者又依赖于 walkPrototypeChain
。如果需要,它们显然可以折叠成一个函数。
const walkPrototypeChain = (process, init, finish) => (obj) => {
let currObj = obj, currRes = init();
do {
currRes = process(currRes, currObj)
} while (currObj = Object.getPrototypeOf(currObj))
return finish(currRes)
}
const allKeys = walkPrototypeChain(
(set, obj) => {Object.getOwnPropertyNames(obj).forEach(k => set.add(k)); return set},
() => new Set(),
s => [...s]
)
const bindMethods = (obj) => allKeys(obj).reduce(
(o, n) => typeof obj[n] == 'function' ? ({...o, [n]: obj[n].bind(obj)}) : o,
{}
)
const date = new Date()
const {getDate, getFullYear} = bindMethods(date) // or any other date function
console.log(getDate())
console.log(getFullYear())