javascript - "TypeError: Cannot create proxy with a non-object as target"的 ES6 代理解决方法?

标签 javascript object inheritance reflection functional-programming

我的最后一个问题: 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
 

虽然我认为这是个好主意,但另一位成员认为这是不一致的,因为 Object.assign是一个可变操作,在函数式编程上下文中不应被允许。

作为回应,还有一个思路是用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
 

我也考虑过利用Object.create , 但不能以类似 Proxy 的方式工作。

基本上,我认识到这是实现 Inheritance (object-oriented programming) 的一个挑战在使用动态类型的函数式编程中/duck typingreflection .

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

有什么好主意吗?谢谢。

最佳答案

事实证明,

  • 符号()
  • Object.assign()
  • 代理

所有这些都不是实现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
);

关于javascript - "TypeError: Cannot create proxy with a non-object as target"的 ES6 代理解决方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51343530/

相关文章:

javascript - Firebase 数据库 : how to get first record of each list

javascript - 页脚JS调整问题

javascript - 如何获取 AngularJS ngHandsontable 的句柄

java - 如何迭代从对象派生的 ArrayList

inheritance - 为什么我的嵌套 div 不继承父级高度?

java - 如何在子类上下文中执行从父类(super class)继承的方法?

javascript - 表格: password with images?

javascript - 有没有一种简单的方法可以在 Javascript 中完全卡住对象及其子对象(Deep Freeze)?

c++ - 在 XY 点数组中找到三个 XY 点的最大面积

java - 对Java继承规则的困惑