在JavaScript中创建新函数(带有预填充参数)的这两种方法之间有什么区别吗?
我试图在这里完成的是提供一组函数,每个函数都基于在JavaScript中创建新函数(带有预填充参数)的这两种方法之间有什么区别吗?,javascript,Javascript,我试图在这里完成的是提供一组函数,每个函数都基于apiCallone(它具有查询参数处理、URL格式设置等逻辑),但带有预填充的参数 因此,我在getProducts和getCategories中使用了bind,但在getProduct中也使用了匿名函数,因为我需要传递的id值 我想知道这两种方式之间是否有任何缺点、问题或甚至任何区别 // ... // apiVersion, storeId and accessToken are provided const boundApiCall = a
apiCall
one(它具有查询参数处理、URL格式设置等逻辑),但带有预填充的参数
因此,我在getProducts
和getCategories
中使用了bind
,但在getProduct
中也使用了匿名函数,因为我需要传递的id
值
我想知道这两种方式之间是否有任何缺点、问题或甚至任何区别
// ...
// apiVersion, storeId and accessToken are provided
const boundApiCall = apiCall.bind(null, apiVersion, storeId, accessToken);
return {
apiVersion,
storeId,
accessToken,
apiCall: boundApiCall,
getProduct: id => boundApiCall.call(null, `products/${id}`),
getProducts: boundApiCall.bind(null, 'products'),
getCategories: boundApiCall.bind(null, 'categories'),
};
我假设您是按照
obj.getProduct(42)
和obj.getCategories()
的思路使用它的,对吗?是的,某种程度上-谢谢您的提问