Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从ImmutableJS记录构造函数调用函数_Javascript_Typescript_Immutable.js - Fatal编程技术网

Javascript 如何从ImmutableJS记录构造函数调用函数

Javascript 如何从ImmutableJS记录构造函数调用函数,javascript,typescript,immutable.js,Javascript,Typescript,Immutable.js,为了某种程度的安全性/安全性/温暖性和模糊性,我已经用不可变的记录替换了我的许多/大部分typescript实体,但我刚刚注意到我的构造函数现在坏了 最初,我在构造函数中自动创建新的UUID作为默认值,但使用ImmutableJS记录,这种行为被破坏了 我确实理解为什么,但我不完全确定正确的解决方法是什么——但我觉得它要么非常复杂,要么非常简单 import { Record } from "immutable"; import uuid from "uuid"; const tagDefau

为了某种程度的安全性/安全性/温暖性和模糊性,我已经用不可变的记录替换了我的许多/大部分typescript实体,但我刚刚注意到我的构造函数现在坏了

最初,我在构造函数中自动创建新的UUID作为默认值,但使用ImmutableJS记录,这种行为被破坏了

我确实理解为什么,但我不完全确定正确的解决方法是什么——但我觉得它要么非常复杂,要么非常简单

import { Record } from "immutable";
import uuid from "uuid";

const tagDefaults: TagParams = {
    depth: 0,
    id: "tag::" + uuid.v4(),
    name,
    order: 0,
};

interface TagParams {
    name: string;
    order: number;
    depth: number;
    id: string;
}

export class Tag extends Record(tagDefaults) { }
创建初始的
tagDefaults
是创建第一个UUID的过程-之后,所有后续的新
Tag()
都使用相同的ID

有没有一种简单的方法可以在每个构造函数上调用函数?我曾尝试在构造函数中重写
(this.id=uuid.v4())
,但这实际上会导致Webpack对我产生影响

更新日期:2018年6月8日

使用提供的答案@mpontus,下面是一个更新的示例,显示了任一选项都可以工作

import { Record, Set } from "immutable";
import uuid from "uuid";

const tagDefaults: TagParams = {
    depth: 0,
    id: "",
    name,
    order: 0,
};

interface TagParams {
    name: string;
    order: number;
    depth: number;
    id: string;
}

export class Tag extends Record(tagDefaults) {
    constructor(props: Partial<TagParams> = {}) {
        // Option A - Works
        if (!props.id) {
            props.id = uuid.v4();
        }

        super(props);

        // Option B - Works
        // if (!this.id) {
        //     return this.set("id", uuid.v4());
        // }
        // return this;
    }
}

describe("Given a Tag", () => {
    describe("When constructed", () => {
        test("It should contain a unique id", () => {
            const tag1 = new Tag();
            const tag2 = new Tag({});
            const tag3 = new Tag({ depth: 1, name: "hello", order: 10 });
            const tag4 = new Tag({ id: "tag4Id" });
            const tags = Set([tag1, tag2, tag3, tag4].map((t) => t.id));
            expect(tags.size).toBe(4);
            console.log([tags]);
        });
    });
});
从“不可变”导入{Record,Set};
从“uuid”导入uuid;
常量标记默认值:标记参数={
深度:0,
id:“”,
名称
订单:0,
};
接口标记参数{
名称:字符串;
顺序:编号;
深度:数字;
id:字符串;
}
导出类标记扩展记录(标记默认值){
构造函数(props:Partial={}){
//方案A-有效
如果(!props.id){
props.id=uuid.v4();
}
超级(道具);
//方案B-工程
//如果(!this.id){
//返回这个.set(“id”,uuid.v4());
// }
//归还这个;
}
}
描述(“给定标签)”,()=>{
描述(“构建时”,()=>{
测试(“它应该包含唯一的id”,()=>{
const tag1=新标记();
const tag2=新标记({});
const tag3=新标记({深度:1,名称:“hello”,顺序:10});
const tag4=新标记({id:“tag4Id”});
const tags=Set([tag1,tag2,tag3,tag4].map((t)=>t.id));
期望值(标签、尺寸)为4;
log([tags]);
});
});
});

使用默认值无法实现您想要的功能

您尝试重写标记构造函数的结果很好,但必须重写进入构造函数的值:

const tagDefaults = {
    depth: 0,
    id: "",
    name: "",
    order: 0,
};

class Tag extends Record(tagDefaults) {
    constructor(values) {
        const finalValues = { ...values };

        if (finalValues.id === undefined) {
            finalValues.id = uuid.v4();
        }

        super(finalValues);
    }
}
或者,您可以从构造函数返回不同的记录实例,但我不确定TypeScript是否会接受

const { Record } = require("immutable");
const uuid = require('uuid');

const tagDefaults = {
    depth: 0,
    id: undefined,
    name: "",
    order: 0,
};

class Tag extends Record(tagDefaults) {
    constructor(values) {
        super(values);

        if (this.id === undefined) {
            return this.set('id', uuid.v4());
        }

        return this;
    }
}

谢谢我相信我已经尝试了第一个或第二个版本,但它根本不起作用(不记得这是运行时错误还是编译错误),但我会尝试使用您的代码并报告回来。结果表明,这两个选项都起作用-这太疯狂了,因为我可以发誓我以前已经测试过其中一个。不管怎样。。。快乐的日子!用答案更新了我的问题