ES6 proxy work-around for “TypeError: Cannot create proxy with a non-object as target”?

泄露秘密 提交于 2019-12-14 03:28:22

问题


My last question here: How to store data of a functional chain of Monoidal List?

had many great responses and one of them suggested to implement some sort of type structure in 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
 

While I thought this is a great idea, another member suggests this is inconsistent since Object.assign is a mutable operation and it should not be allowed in Functional Programming context.

In response, there is another idea to use Proxy instead, so I have tried to implement the similar system by myself.

The result is unfortunately very poor since Proxy seems only to accept Object.

Works as the expample

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
 
"Uncaught TypeError: Cannot create proxy with a non-object as target or handler"

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
 

I also considered to take advantage of Object.create, but does not work in similar manner of Proxy.

Basically, I recognize this is a challenge to implement Inheritance (object-oriented programming) in functional programming with dynamic typing/ duck typing and reflection.

Therefore, I really want to make this implementation work in ES6 Proxy context.

Any great ideas? Thanks.


回答1:


It turns out,

  • Symbol()
  • Object.assign()
  • Proxy

all of them are not necessary to implement Reflection (computer programming) at least for this topic.

See my code below:

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
);


来源:https://stackoverflow.com/questions/51343530/es6-proxy-work-around-for-typeerror-cannot-create-proxy-with-a-non-object-as-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!