在javascript中复制python的__call__?

前端之家收集整理的这篇文章主要介绍了在javascript中复制python的__call__?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想复制实例化一个可调用的类而不使用模块模式.

以下是我最好的尝试.但是,它使用__proto__,我不确定.这可以在没有__proto__的情况下完成吗?

  1. function classcallable(cls) {
  2. /*
  3. * Replicate the __call__ magic method of python and let class instances
  4. * be callable.
  5. */
  6. var new_cls = function () {
  7. var obj = Object.create(cls.prototype);
  8. // create callable
  9. // we use func.__call__ because call might be defined in
  10. // init which hasn't been called yet.
  11. var func = function () {
  12. return func.__call__.apply(func,arguments);
  13. };
  14. func.__proto__ = obj;
  15. // apply init late so it is bound to func and not cls
  16. cls.apply(func,arguments);
  17. return func;
  18. }
  19. new_cls.prototype = cls.prototype;
  20. return new_cls
  21.  
  22. }

解决方法

代理

如果您反对使用__proto__或与之相关的任何内容但仍希望继承,则可以使用Proxies.

  1. var ObjectCallable_handler = {
  2. get: function get(self,key) {
  3. if (self.hasOwnProperty(key)) {
  4. return self[key];
  5. } else { return self.__inherit__[key]; }
  6. },apply: function apply(self,thisValue,args) {
  7. return (self.__call__ || self.__inherit__.__call__).apply(self,args);
  8. }
  9. };
  10.  
  11. function ObjectCallable(cls) {
  12. var p = new Proxy(function() { },ObjectCallable_handler);
  13. p.__inherit__ = cls;
  14. return p;
  15. }

优点

>保持可继承性.
>不涉及传统的原型链.
> ES6 draft.

缺点

> Currently supported仅限Firefox> = 24.

setPrototypeOf

如果缺乏对Proxies的支持令您失望,您可以尝试使用setPrototypeOf polyfill.

  1. Object.setPrototypeOf = Object.setPrototypeOf || function (obj,proto) {
  2. obj.__proto__ = proto;
  3. return obj;
  4. }
  5.  
  6. function ObjectCallable(cls) {
  7. var f = function() { return f.__call__.apply(f,arguments); };
  8. Object.setPrototypeOf(f,cls);
  9. return f;
  10. }

优点

>可能现在和将来都做得最好.
> ES6 draft.

缺点

>通过polyfill使用非标准的__proto__,并且需要在您想要支持的引擎/浏览器之间进行测试.
> setPrototypeOf在任何浏览器中都是not currently implemented.

复制

这是功能最少的最简单的解决方案,但几乎可以在任何地方使用.创建一个新函数并将对象的属性克隆到其上.

  1. function ObjectCallable(cls) {
  2. var f = function() { return f.__call__.apply(f,arguments); },k;
  3. for (k in cls) {
  4. f[k] = cls[k];
  5. }
  6. return f;
  7. }

优点

>几乎无处不在.

缺点

>不保留任何继承或原型结构的外观.

结论

在JavaScript中复制Python的__call__功能要做的事情需要更多时间,因为ES6开发并在更多引擎中实现或依赖于非标准功能,如__proto__.

猜你在找的JavaScript相关文章