How does nodejs interpret key value pairs when no semi color exists and the key itself is a string called with a function?

huangapple go评论57阅读模式
英文:

How does nodejs interpret key value pairs when no semi color exists and the key itself is a string called with a function?

问题

以下是翻译好的部分:

我只是在浏览hexo-cli中的一些测试代码,发现了下面的代码行(完整的代码和存储库可以在[这里][1]找到):

    const hexo = proxyquire('../../dist/hexo', {
      './console'(ctx) {
        ctx.extend.console.register('help', spy);
      }
    });

我已经阅读了在线文章,该模块可以使用`proxyquire`进行代理([proxyquery][2]),然后可以传递键值以模仿这些模块中的方法,例如以下示例:

    var calculateDiscounts = proxyquire(‘./somemodule’, {
      ‘*/cartridge/scripts/hooks/cart/calculateHelpers’: {
        removeAllPriceAdjustments: removeAllPriceAdjustmentsStub
      },
      ‘dw/campaign/PromotionMgr’: {
        getPromotion: getPromotionStub,
        getDiscounts: getDiscountsStub,
        applyDiscounts: applyDiscountsStub
      }
    });

([带有上述代码片段的原始文章][3])

那么下面的代码行是如何执行的呢?为什么没有抛出错误,因为它看起来根本不像一个有效的对象?

    './console'(ctx) {
       ctx.extend.console.register('help', spy);
    }

  [1]: https://dn.myshopmatic.com/images/7ZEO4V/Rn2JYwED4q.jpg
  [2]: https://www.npmjs.com/package/proxyquire
  [3]: https://medium.com/ordergroove-engineering/unit-testing-with-chai-proxyquire-and-sinon-df0ecba75245

希望这个翻译对你有所帮助。

英文:

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);
}

答案1

得分: 1

这与
```javascript
'./console': function(ctx) {
   ctx.extend.console.register('help', spy);
}

更多信息请参见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer?retiredLocale=pl#method_definitions 或 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions

在浏览器中正常工作:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    const a = {
      './console'(num) {
        return num * 5
      }
    }

    console.log(a['./console'](2))


<!-- end snippet -->
英文:

This is the same as

&#39;./console&#39;: function(ctx) {
   ctx.extend.console.register(&#39;help&#39;, 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:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const a = {
  &#39;./console&#39;(num) {
    return num * 5
  }
}

console.log(a[&#39;./console&#39;](2))

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月23日 22:36:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定