如何为 node.js 插件创建包装类 [英] How to create a wrapper class for node.js plugin

查看:24
本文介绍了如何为 node.js 插件创建包装类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是 JavaScript 的新手,并且有一个我使用 Node.js 的项目cookie 模块在这里找到.我已经能够创建 cookie &像这样正确设置它们:

So I'm new to JavaScript, and have a project where I'm using the Node.js cookies module found here. I've been able to create cookies & set them correctly like so:

this.Vue.$cookies.set('cookieName', cookieValue, {
          sameSite: 'lax',
          secure: true
});

但是,我想创建一个包装函数/类,以便我可以将 sameSite: 'lax'secure: true 选项设置为默认值,而不必每次我从该模块调用 set 函数时都将它们传入.显然,如果我愿意,我也希望能够将这些选项覆盖为其他选项.

However, I want to create a wrapper function/class so I can set the sameSite: 'lax' and secure: true options as defaults, and not have to pass them in every time I call the set function from that module. Obviously, I also want to be able to overwrite those options to something else if I want.

查看我在别处找到的示例,我认为该类应该模糊如下所示:

Looking through examples I've found elsewhere, I think the class should look something vaguely like this:

const cookies = require('cookie-universal-nuxt');

cookies(function ($) {

    const defaultOptions = {
        sameSite: 'lax',
        secure: true
    };

    return {
        get: function(name, options) {
            return $.cookies(name, options)
        },

        set: function (name, value, options) {
            $.cookies(name, value, // somehow options & defaultOptions are passed in and merged here)
        },
    };
});

然而,这可能是完全错误的.就像我说的,我是 JS 新手,所以我很迷茫.任何帮助将不胜感激!

However, this might be totally wrong. Like I said, I'm new to JS, so I'm pretty lost. Any help would be greatly appreciated!

推荐答案

我认为您不需要为此创建一个类 - 一个简单的包装函数可能会满足您的需求.下面是如何创建一个小工厂函数来创建一个绑定到特定上下文的包装器:

I don't think you need a class for this - a simple wrapper function will probably do what you need. Here's how to create a little factory function that creates a wrapper bound to a specific context:

function makeSetCookie($) {
  return function(name, value, options) {
    const defaultOptions = {
      sameSite: 'lax',
      secure: true
    }; 

    $.set(name, value, Object.assign(defaultOptions, options));
 }
}

const setCookie = makeSetCookie(this.Vue /* or whatever */);
// and now you can use setCookie wherever

调用者可以传递或不传递 options 参数的值.Object.assign(defaultOptions, options) 行将所有键/值从 options 复制到 defaultOptions,覆盖 defaultOptions 中已有的任何键(相同站点,安全),如果它们出现在 options 中.如果 options 为 null 或未定义,那很好 - 返回的值只是 defaultOptions.更多信息请访问:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

The caller can pass or not pass a value for the options param. The line Object.assign(defaultOptions, options) copies all keys/values from options to defaultOptions, overwriting any keys already in defaultOptions (sameSite, secure) if they appear in options. If options is null or undefined, that's fine - the value returned will just be defaultOptions. More info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

这篇关于如何为 node.js 插件创建包装类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆