将循环结构转换为JSON

时间:2015-10-13 20:26:05

标签: javascript arrays object local-storage stringify

我有两种物体,Beam和Sample。样本包含2个光束,我有一个样本数组。我需要将数组存储到本地存储中,所以我调用localStorage["samples"] = JSON.stringify(samples);但是我得到错误“将循环结构转换为JSON”。我的对象不包含自己。我也尝试用1 samples对象替换beam对象,但得到相同的错误,而Beam只有整数和字符串值。

修改

以下是对象。

    function FlexuralStrengthT97(result, method, beam1, beam2, waitForCuring, averageBeams) {
        this.Result = result;
        this.Method = method;
        this.Beam1 = beam1;
        this.Beam2 = beam2;
        this.WaitForCuring = waitForCuring;
        this.AverageOfBeams = averageBeams;

        return this;
    }


    function FSBeam(testingMachineId, beamAge, widthU, widthC, widthL, widthAverage, depthR, depthC, depthL, depthAverage, maxLoad, fs, psi, breakOutside) {
        this.TestingMachineId = testingMachineId;
        this.BeamAge = beamAge;
        this.WidthUpper = widthU;
        this.WidthCenter = widthC;
        this.WidthLower = widthL;
        this.WidthAverage = widthAverage;
        this.DepthRight = depthR;
        this.DepthCenter = depthC;
        this.DepthLeft = depthL;
        this.DepthAverage = depthAverage;
        this.MaxLoad = maxLoad;
        this.FS = fs;
        this.PSI = psi;
        this.BreakOutside = breakOutside;

        return this;
    }

1 个答案:

答案 0 :(得分:1)

这些似乎是构造函数,请确保将它们与new关键字一起使用:

var beam1 = new FSBeam();
var flex = new FlexuralStrengthT97();

否则,this将是window而不是实例范围。