将C#3D阵列移植到JS 3D阵列 [英] Porting C# 3D Array to JS 3D Array

查看:57
本文介绍了将C#3D阵列移植到JS 3D阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个ac#library,它使用了3个项目,在那个特定的代码片段中继续,但是,我仍然需要在javascript中使用那个代码,所以我把它移植出来,事情是,我不认为我可以复制相同的逻辑,例如,我已经考虑了几天,我无法得到答案。

I developed a c# library which used across 3 project that relay in that specific piece of code, though, I still need to use that code in a javascript, so im porting it out, the thing is, I don't think I can replicate the same logic, for example, I've been thinking about this for a few days I couldn't get to the answer.

在C#库中我有一个3D数组有点像应用程序中的核心属性,我似乎无法弄清楚如何在JS环境中使用它。

In the C# library I have a 3D array which is kinda like the core property in the application, and I cannot seem to figure out how to make that work in JS environment.

例如我有这段代码:

public Obj[,,] objs = new Obj[18, 14, 8];

我会在每个单独的位置分配对象,并尝试将其移植到javascript,貌似会结果是:

In which I would allocate objects in EVERY single position, and trying to port that to javascript, seemingly would result in:

var 3dArr = new Array();
    3dArr[0] = new Array();
    3dArr[0][0] = new Array();

这总是将数组对象保持在第一个位置,如果我设置了其他任何我将失去的整个阵列?或者我错了

Wouldnt this always hold the array object in the first position and if I set whatever else I would lose the whole array? Or am I wrong

推荐答案

在撰写本文时,JavaScript中没有与C#中的多维数组非常相似的语言功能。也不建议将普通数组暴露给整个应用程序的使用,因为通过向数组写入一些它不应该保存的东西会很容易错误地损害整个代码。

At the time of writing, there is no language feature in JavaScript that would closely resemble multidimensional arrays in C#. It is also not recommended to expose ordinary arrays for the usage of whole application as that would make it easy to mistakenly harm the whole code by writing to the array something it is not supposed to hold.

但是,将所需的数组封装到一个简单的新类中应该相对容易,例如以下代码中的Array3d:

However, it should be relatively easy to encapsulate an array you need into a simple new class, e.g. Array3d in the code below:

/***** CODE OF THE ARRAY *****/
function Array3d(width, height, depth, type) {
    this.width = width;
    this.height = height;
    this.depth = depth;
    this.type = type;
    this.array = new Array(width * height * depth);
}

Array3d.prototype = {
    get: function(x, y, z) {
        return this.array[calcIndex(this, x, y, z)];
    },
    set: function(x, y, z, item) {
        if (!(item instanceof this.type)) {
            throw new Error(item + ' is not instance of ' + this.type + '.');
        }
        this.array[calcIndex(this, x, y, z)] = item;
    }
};

function calcIndex(array3d, x, y, z) {
    return x + array3d.width * y + array3d.width * array3d.height * z;
}

/***** USAGE CODE *****/
function Something(i) {
    this.index = i;
}

var array3d = new Array3d(10, 11, 12, Something);
var something = new Something(11);
array3d.set(4, 0, 0, something);
var gettingBack = array3d.get(4, 0, 0);
if (gettingBack === something) {
    console.log('1: Works as expected');
}
else {
    console.error('1: Not expected ' + gettingBack);
}
gettingBack = array3d.get(0, 0, 4);
if (gettingBack == null) {
    console.log('2: Works as expected');
}
else {
    console.error('1: Not expected ' + gettingBack);
}

这篇关于将C#3D阵列移植到JS 3D阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆