ES6 代理解决“TypeError: Cannot create proxy with a non-object as target”?

2024-02-01

我的最后一个问题是:如何存储Monoidal List函数链的数据? https://stackoverflow.com/questions/51297054/how-to-store-data-of-a-functional-chain-of-monoidal-list

有很多很好的回应,其中之一建议在 JavaScript 中实现某种类型结构:

 const TYPE = Symbol();
  const typeOf = t => x => x == null
    ? x
    : Object.assign(x, {
      [TYPE]: t
    });

  const isType = t => x => x == null
    ? false
    : x[TYPE] === t;

  const Foo = x => typeOf(Foo)(x);

  console.log(
    isType(Foo)(1) // false
    , isType(Foo)([]) // false
    , isType(Foo)({}) // false
    , isType(Foo)(x => x) // false
    , isType(Foo)(true) // false
    , isType(Foo)(undefined) // false
    , isType(Foo)(null) // false
  );
 
  console.log(
    isType(Foo)(Foo(1)) // true
    , isType(Foo)(Foo([])) // true
    , isType(Foo)(Foo({})) // true
    , isType(Foo)(Foo(x => x)) // true
    , isType(Foo)(Foo(true)) // true
    , isType(Foo)(Foo(undefined)) // false
    , isType(Foo)(Foo(null)) // false
  );

  console.log(Foo(1) + Foo(2)); //3
 

虽然我认为这是一个好主意,但另一位成员认为这是不一致的,因为对象.分配 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign是一个可变操作,在函数式编程上下文中不应允许它。

作为回应,还有另一个想法可以使用Proxy https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy相反,所以我尝试自己实现类似的系统。

不幸的是,结果很差,因为Proxy似乎只接受 Object.

以作品为例

var target = {};
var p = new Proxy(target, {});

p.a = 37; // operation forwarded to the target

console.log(target.a); // 37. The operation has been properly forwarded
 

“未捕获的类型错误:无法使用非对象作为目标或处理程序创建代理”

var target = 5;
var p = new Proxy(target, {});

p.a = 37; // operation forwarded to the target

console.log(target.a); // 37. The operation has been properly forwarded
 

我也考虑过利用对象.create https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create,但不以类似的方式工作Proxy.

基本上,我认识到这是一个实施起来的挑战继承(面向对象编程) https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)在具有动态类型的函数式编程中/鸭子打字 https://en.wikipedia.org/wiki/Duck_typing and 反射 https://en.wikipedia.org/wiki/Reflection_(computer_programming).

因此,我真的想让这个实现在 ES6 Proxy 上下文中工作。

有什么好主意吗?谢谢。


事实证明,

  • Symbol()
  • 对象.分配()
  • Proxy

所有这些都没有必要实施反思(计算机编程) https://en.wikipedia.org/wiki/Reflection_(computer_programming)至少对于这个话题来说。

请参阅下面我的代码:

const selfAware = i => i[i] = i;
const isAware = i => (i[i] === i);

const I = i => (i === I) || (i == null)
  ? i
  : selfAware(Object(i));

const amI = i => (i === I)
  ? true
  : (i == null)
    ? false
    : isAware(i);

const ss = I(6);

console.log(ss);

console.log(ss[ss]);
//self-similarity
console.log(ss[ss][ss]);


const obj1 = {
  a: 2
};
const obj2 = I(obj1);
console.log(obj1);
console.log(obj2);

console.log(
  I("Hello world!").toString() //Yes, it works!
);

console.log(I(1) + I(2)); //3

console.log(
  (I) //[Function: I]
);
console.log(
  (I)(I) //[Function: I]
);
console.log(
  (I)(I)(I) //[Function: I]
);
console.log(
  (I)(I)(I)(I) //[Function: I]
);
console.log("============================");


console.log(
  amI(I) //true
  , amI(1) // false
  , amI([]) // false
  , amI({}) // false
  , amI(x => x) // false
  , amI(true) // false
  , amI(false) // false
  , amI(undefined) // false
  , amI(null) // false
);

console.log(
  amI(I(I)) // true
  , amI(I(1)) // true
  , amI(I([])) // true
  , amI(I({})) // true
  , amI(I(x => x)) // true
  , amI(I(true)) // true
  , amI(I(false)) // true
  , amI(I(undefined)) // false
  , amI(I(null)) // false
);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ES6 代理解决“TypeError: Cannot create proxy with a non-object as target”? 的相关文章

随机推荐