问题描述
我在这里的最后一个问题:如何存储Monoidal功能链的数据列表?
My last question here:How to store data of a functional chain of Monoidal List?
有很多不错的响应,其中之一建议在JavaScript中实现某种类型的结构:
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
虽然我认为这是个好主意,但另一位成员却认为这是不一致的,因为 Object.assign 是可变操作,在函数式编程上下文中不应允许.
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.
不幸的是,由于Proxy
似乎只接受Object,所以结果很差.
The result is unfortunately very poor since Proxy
seems only to accept 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
类似的方式工作.
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.
因此,我真的很想使此实现在ES6代理上下文中起作用.
Therefore, I really want to make this implementation work in ES6 Proxy context.
有什么好主意吗?谢谢.
Any great ideas? Thanks.
推荐答案
事实证明,
- Symbol()
- Object.assign()
- 代理
所有这些都不是实现反射(计算机编程)所必需的至少对于这个主题而言.
all of them are not necessary to implement Reflection (computer programming) at least for this topic.
请参阅下面的代码:
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
);
这篇关于ES6的"TypeError:无法创建将非对象作为目标的代理"的代理变通办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!