나는 "아마 bmDup은 함수가 아니다"라는 오류를 알기는하지만 정보가 아마도 어쩌면 내가 검색 할 필요가 있다고 알려주는지 이해하지 못한다.
함수에 랩핑하면 반환 값이 정의되지 않습니다.모나드 바인딩 방법
function Maybe() {
Object.freeze(this);
};
function Just (x) {
this.toString = function() { return "Just " + x.toString(); };
this.just = x;
Object.freeze(this);
};
Just.prototype = new Maybe();
Just.prototype.constructor = Just;
function Nothing() {
this.toString = function() { return "Nothing"; };
Object.freeze(this);
};
Nothing.prototype = new Maybe();
Nothing.prototype.constructor = Nothing;
Maybe.unit = function (x) {
// return a Maybe that holds x
return new Just (x);
};
Maybe.bind = function (f) {
// given a function from a value to a Maybe return a function from a Maybe to a Maybe
return new Maybe(f(this.just));
};
//to test
function mDup(str) {
return new Just(str+str);
}
console.log(mDup("abc")); // => new Just("abcabc") OK
var bmDup = Maybe.bind(mDup);
console.log(bmDup(new Just("abc"))) // => new Just("abcabc") NOK
엄격한 모드를 시도해야합니다. 따라서 이러한 모든 실수는 예외로 나타납니다. – Bergi