如何重写模块函数'

How to override Module Function's Implementation

本文关键字:函数 模块 何重写 重写      更新时间:2023-09-26

我有以下模块:

car.js

module.exports = {
    gateway: function(gateway){
        _gateway = gateway
    },
    find: function *(carRequest){
        var cars = _gateway.find(carRequest.id).next().value;
        carResponse.cars = cars;
        yield countryResponse;
    }
};

我需要能够嘲笑这个。我希望能够重写find(id)的实现。由于这不是一种类型语言,我如何在Node和JS中做到这一点?

使用下面的代码可以轻松完成您想要的内容。

假设模块名为car

var car = require('car');
car.find = function(carRequest) {
    console.log('hello');
    return false;
}
car.find();//this will print 'hello'