简体   繁体   English

如何包装 class 以便默认情况下所有方法都传递某个额外参数?

[英]How can I wrap a class so that its methods are all passed a certain extra parameter by default?

Let's say I have the following class that's been provided by an external library:假设我有以下由外部库提供的 class :

class ExampleClass {
    methodA(a, b, c) {}
    methodB(a, b, c) {}
    methodC(a, b, c) {}
}

How can I wrap this class, so that a value to parameter C is always provided, such that I can call the methods as normal, but leave out parameter C?如何包装这个 class,以便始终提供参数 C 的值,以便我可以正常调用方法,但省略参数 C?

For example, let's assume parameter C is 20 by default, I can then do this:例如,假设参数 C 默认为 20,那么我可以这样做:

new ExampleClass().methodA(12, 14)

And the resulting parameters are:结果参数是:

a=12, b=14, c=20

new ExampleClass().methodB(39, 12)

And the resulting parameters are:结果参数是:

a=39, b=12, c=20

new ExampleClass().methodC(18, 14)

And the resulting parameters are:结果参数是:

a=18, b=14, c=20

I've been doing some reading into ES6 Proxies, looks like apply() could work here, but I can't seem to get the implementation right.我一直在阅读 ES6 代理,看起来 apply() 可以在这里工作,但我似乎无法正确实现。

EDIT: Also note that the method names are unpredictable and dynamically generated.编辑:另请注意,方法名称是不可预测的并且是动态生成的。 I need to generically apply this.我需要一般地应用这个。 I can't extend the class and use super.我无法扩展 class 并使用超级。

Example with a Proxy : Proxy示例:

 class ExampleClass { methodA(a, b, c) {console.log('A', a, b, c)} methodB(a, b, c) {console.log('B', a, b, c)} methodC(a, b, c) {console.log('C', a, b, c)} } function ExampleClassWithC(obj, c) { return new Proxy(obj, { get(target, p) { if (typeof target[p] === 'function') return (a, b) => target[p](a, b, c) return target[p] } }) } let c = ExampleClassWithC(new ExampleClass, 'myC') c.methodA(1, 2) c.methodB(3, 4) c.methodC(5, 6)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何自动包装类的所有方法? - How can I automatically wrap all the methods of a class? 我可以检测传递给javascript方法的未使用的额外参数吗? - Can I detect unused extra parameters passed to javascript methods? 如何格式化CSS和Bootstrap,以便在页面宽度减小时仅包含某些元素? - How can I format my CSS and Bootstrap so that only certain elements wrap when page width decreases? 为什么javascript函数的作用域作为参数'window'传递 - 我如何将作用域作为父类? - Why is the scope of a javascript function passed as a parameter 'window' — and how do I make the scope its parent class? 如何根据传递给方法参数的数字为数组定义新的原型方法以进行计数? - How can I define a new prototype methods to arrays for count based the number passed to the method parameter? 如何将某个类的所有元素包装为不同的类? - How to wrap all alements of a certain class for different classes? 如何以编程方式替换(包装)新方法中的方法? - How can I replace (wrap) methods in new methods programmatically? 如何在下一次出现相同元素之前包装每个元素及其所有兄弟元素? - How can I wrap each element and all its siblings before the next occurrence of the same element? 我无法将 jQuery object 包裹在传递给自己的锚点 onclick ZC1C425268E68385D1AB507ZFC17A - I can't wrap jQuery object around anchor passed to its own onclick function Typescript:如果派生类中未传递任何值,如何设置可选参数? - Typescript: How can I set an optional parameter if no value is passed in a derived class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM