从javascript对象返回多个变量的正确方法

时间:2015-11-02 00:35:26

标签: javascript

我目前正在学习更多关于javascript对象的内部工作原理,并且遇到了一个从对象返回多个变量的示例:

var squareGrid = function(width, n, z) {
        width = width || 1;
        n = n || 4;
        var z = z || width / 2.0;

        var edges = [],
            pts = [];

        var step = width / n;
        for (var u = 0; u < n; u++) {
            var u_added = u * n * 4;
            for (var v = 0; v < n; v++) {
                var delta = u_added + 4 * v;

                var t_v = step * (v - n / 2);
                var t_u = step * (u - n / 2);

                pts.push([t_v, t_u, z]); // top left
                pts.push([t_v, t_u + step, z]); // top right
                pts.push([t_v + step, t_u + step, z]); // bottom right
                pts.push([t_v + step, t_u, z]); // bottom left

                edges.push([delta + 0, delta + 1]);
                edges.push([delta + 1, delta + 2]);
                edges.push([delta + 2, delta + 3]);
                edges.push([delta + 3, delta + 0]);
            }

        }
        return {
            edges: edges,
            pts: pts
        };
    }

在这种情况下,为了返回edgespoints,看起来返回键值和值是相同的键值对。这种方法是必要的还是可以完成以下任务?

return{ edges, pts}};

1 个答案:

答案 0 :(得分:3)

您将返回一个对象,其中键是边缘,值将是边的返回值。

- (NSString *)getClassNamesWithClassID: (NSNumber *) classID {
    NSLog(@"Gettting Name for classID: %@", classID);

    NSURLRequest *classIDRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://dphs.edu20.org/api/get_classes_with_ids?api_key=%@&classIDs=%@", apiKey, classID]]];

    NSURLConnection *classIDConnection = [[NSURLConnection alloc] initWithRequest:classIDRequest delegate:self];

    return className;
}

您可以执行以下操作:data = callfunction() - &gt;返回[edge,points]

NSURLConnection

**更正** 当密钥存储在js散列中时,它会在尝试存储值

之前序列化密钥

将存储的是数组的序列化版本作为键,值为数组