文章目录
本篇文章将介绍如何使用不同的方法在 JavaScript 中添加两个向量。
在 JavaScript 中使用 for 循环添加向量类
您可以使用 for 循环在 JavaScript 中添加两个向量。 同时,向量应该是 JavaScript 数组。
定义一个构造函数,它将执行加法并返回结果以开始。 最好将加法函数与构造函数分开编写,以使代码干净并可在其他项目中重用。
加法函数应该起作用,以便第一个向量可以在第二个向量上调用它。
因此,第一个向量是调用者,第二个是被调用者。 因此,加法函数应按如下方式工作:
- 确保参数是相同长度的数组。 参数是第一个向量(调用者)和第二个数组(被调用者)。
- 构造一个空数组来存储向量加法的最终结果。
- 循环调用者数组。
- 使用循环索引添加调用方数组和被调用方数组元素。
- 将结果推入步骤 1 创建的空数组。因此,它不再为空,因为它具有加法的结果。
- 使用新数组返回构造函数的新实例。
- 如果在此过程中出现问题,请返回错误。
下面的代码是这些步骤的实现。 我们实施了额外的检查,以确保用户传递正确的矢量值。
我们添加了两个向量来测试向量加法并将结果记录到 Web 浏览器的控制台。
代码:
function Vector(arg) {
this.array = arg;
this.add = add;
}
function add(called_array) {
if (Array.isArray(this.array) && Array.isArray(called_array.array)) {
if (this.array.length === called_array.array.length) {
let result = [];
for (let i = 0; i < this.array.length; i++) {
if (typeof(this.array[i]) == "number" && typeof(called_array.array[i]) == "number") {
result.push(this.array[i] + called_array.array[i]);
} else {
result.push("Invalid vector value");
}
}
return new Vector(result);
} else {
return new Vector("The vectors are not the same length.");
}
} else {
return new Vector("One of your arguments or both of them is not an array.");
}
}
let caller_array = new Vector([3,5,7]);
let called_array = new Vector ([5,2,9]);
console.log(caller_array.add(called_array).array);
输出:
Array(3) [ 8, 7, 16 ]
在 JavaScript 中使用 Array.proyotype.map() 添加向量类
使用 Array.prototype.map()
添加向量需要向量本身是 JavaScript 数组。 与上一节类似,第一个数组应该是调用者。
这意味着我们将定义一个有助于添加的构造函数。 但是,我们不会将 add()
函数与构造函数分开定义。
这一次,我们将使 add()
函数成为构造函数原型的一部分。
add()
函数将返回构造函数的新实例。 然而,这个新实例的参数是调用数组在被调用数组上使用自定义函数 Array.prototype.map()
的结果。
自定义函数执行数组的添加。
我们使用 Array.prototype.map()
在下面的代码中添加向量。 在此过程中,我们检查了参数是否为相同长度的数组。
除此之外,代码会抛出 TypeError。
代码:
var Vector = function (arg){
this.array = arg;
};
Vector.prototype.add = function(called_array){
called_array = called_array.array;
let caller_array = this.array;
if (Array.isArray(caller_array) && Array.isArray(called_array)) {
if (caller_array.length !== called_array.length) {
throw new TypeError("Vectors have different length");
}
} else {
return new Vector("One of your arguments or both of them is not an array.");
}
return new Vector(caller_array.map(
function (caller_array_element, called_array_element) {
if (typeof(caller_array_element) == "number" && typeof(called_array_element) == "number") {
return caller_array_element + called_array[called_array_element];
} else {
return 'Invalid Vector value';
}
}));
}
let caller_array = new Vector([9,8,5]);
let called_array = new Vector ([2,5,8]);
console.log(caller_array.add(called_array).array);
输出:
Array(3) [ 11, 13, 13 ]
在 JavaScript 中使用 ES6 类添加向量类
使用 ES6 类添加向量背后的基本逻辑是自定义 add()
函数,它使用 for 循环添加向量并返回 ES6 类的新实例。 在添加向量之前,请确保将它们存储在一个数组中。
之后,第一个数组应该在第二个数组上调用 add()
函数。
我们在以下代码中有一个 ES6 类,它使用 add()
函数添加两个数组。 添加后,它将添加的结果传递给 ES6 类的新实例。
代码:
class Vector {
constructor(arr) {
this.arr = arr;
}
add(called_array) {
let caller_array = called_array.arr;
if (Array.isArray(this.arr) && Array.isArray(caller_array)) {
if (this.arr.length === caller_array.length) {
let result = [];
for (let key in this.arr) {
if (typeof(this.arr[key]) == "number" && typeof(caller_array[key]) == "number") {
result[key] = this.arr[key] + caller_array[key];
} else {
result[key] = "Invalid Vector Value.";
}
}
return new Vector(result);
} else {
return new Vector('The vectors are not the same length.');
}
} else {
return new Vector("One of your arguments or both of them is not an array.");
}
}
}
let caller_array = new Vector([3,6,0]);
let called_array = new Vector ([1,5,0]);
console.log(caller_array.add(called_array).arr);
输出:
Array(3) [ 4, 11, 0 ]
通过在 JavaScript 中扩展 Array 类来添加 Vector 类
您可以创建一个扩展 JavaScript 原生 Array 类的矢量类。 因此,向量类应该实现一个函数来添加向量。
第一个向量可以调用第二个向量上的函数。 结果,您将得到加法的结果。
在下面的代码中,我们扩展了原生 Array 类并使用 map()
函数遍历调用的数组。 然后,我们可以将它的元素添加到调用者数组中并产生一个结果。
代码:
class Vector extends Array {
add(called_array) {
if (this.length == called_array.length) {
return this.map((caller_array_element, called_array_element) => {
if (typeof(caller_array_element) == "number" && typeof(called_array_element) == "number"){
return caller_array_element + called_array[called_array_element];
} else {
return 'Invalid Vector value';
};
})
} else {
return "The vectors are not the same length.";
}
}
}
let caller_array = new Vector(1, 2, 4);
let called_array = new Vector(3, 9, 20);
console.log(caller_array.add(called_array));
输出:
Array(3) [ 4, 11, 24 ]