I was just going throw some of the test code in hexo-cli and came across the below lines of code ( full code and repo can be found here ):
const hexo = proxyquire('../../dist/hexo', {
'./console'(ctx) {
ctx.extend.console.register('help', spy);
}
});
I have read articles online stating that the module can be proxied using proxyquiry
( proxyquery ) and then key-values can be passed to imitate the methods in these modules like below for example:
var calculateDiscounts = proxyquire(‘./somemodule’, {
‘*/cartridge/scripts/hooks/cart/calculateHelpers’: {
removeAllPriceAdjustments: removeAllPriceAdjustmentsStub
},
‘dw/campaign/PromotionMgr’: {
getPromotion: getPromotionStub,
getDiscounts: getDiscountsStub,
applyDiscounts: applyDiscountsStub
}
});
( original article with above snippet )
So how does the below line of code execute? Why no errors are thrown, since it doesn't seem like a valid object at all?
'./console'(ctx) {
ctx.extend.console.register('help', spy);
}
This is the same as
'./console': function(ctx) {
ctx.extend.console.register('help', spy);
}
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer?retiredLocale=pl#method_definitions or https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions
Works fine in the broswer:
const a = {
'./console'(num) {
return num * 5
}
}
console.log(a['./console'](2))