我是面向对象的javascript中的新手,并尝试定义一个类,它将数组作为数据成员。这个类的数据成员将另一个类的对象存储为数组。
这个例子会更清楚
function classA(id, objB_01)
{
this.id = id; // data member that store a simple value
this.arrayname = objB_01 // How multiple instance of classB is stored in this array
}
function classB( id, name, status)
{
this.id = id;
this.name = name;
this.status = status
}
objB_01 = new classB("01", "john", "single");
objB_02 = new classB("02", "smith" "single");
objB_03 = new classB("03", "nina", "married");
现在我的问题是如何编写classA
以便classA
的单个实例保存一个存储多个classB
对象的数组
像这样的东西
objA = new classA("01",objB_01);
objA.arrayname = objB_02;
objA.arrayname = objB_03;
现在最后objA
包含一个字符串和一个存储classB
请指出正确的方向
答案 0 :(得分:3)
只需深入了解JavaScript Arrays周围的规范文档,这可能会有所帮助。
除此之外,回答你关于有一个classA的问题,其中一个实例有一个包含多个classB实例的数组 - 我相信你正在寻找数组方法.push()
同样,您可能希望利用JavaScript的标准重载构造函数模式来增强您的类定义。
像这样的东西
function classA(id, objarray)
{
this.id = id; // data member that store a simple value
this.arrayname = objarray || []; //if objarray isn't passed it'll initiate an empty array
}
function classB( id, name, status)
{
this.id = id;
this.name = name;
this.status = status
}
objB_01 = new classB("01", "john", "single");
objB_02 = new classB("02", "smith" "single");
objB_03 = new classB("03", "nina", "married");
var objBarray = [objB_01, objB_02, objB_03];
//now you can use construct & initiate classA in two different ways
//Push each object individually
var objA = new classA("01");
objA.arrayname.push(objB_01);
objA.arrayname.push(objB_02);
objA.arrayname.push(objB_03);
//or push the entire list at once
var objA = new classA("01", objBarray);
答案 1 :(得分:3)
一个选项可以是在构造函数中初始化一个空数组,还有一些方法可以将对象添加到数组中。
function classA (id) {
this.id = id;
this.array = [];
this.add = function (newObject) {
this.array.push(newObject);
};
}
然后你可以这样做:
objA = new classA("01“);
objA.add(objB_01);
objA.add(objB_02);
objA.add(objB_03);
答案 2 :(得分:0)
尝试
var data = {};
var classData = function (id, name, status, options) {
if (options) {
this.id = options.id;
this[options.arrayname] = [];
};
if (!this.hasOwnProperty("id")) {
throw new Error("No id set on `classData` array."
+ "Please set `id` for `classData` at `options` object")
};
var j = (id && name && status) ? JSON.stringify({
id: id,
name: name,
status: status
}) : false;
j ? this[Object.keys(this).filter(function (v) {
return v !== "id"
})[0]].push(JSON.parse(j)) : null;
return this
}.bind(data);
// set `id` at `data` ,
// add first item to `data.arrayname`
classData("01", "abc", "single", {"id":"01", "arrayname":"files"});
// set data at `data`
classData("02", "def", "married");
classData("02", "ghi", "single");
// read `classData`
console.log(classData());
var data = {};
var classData = function (id, name, status, options) {
if (options) {
this.id = options.id;
this[options.arrayname] = [];
};
if (!this.hasOwnProperty("id")) {
throw new Error("No id set on `classData` array."
+ "Please set `id` for `classData` at `options` object")
};
var j = (id && name && status) ? JSON.stringify({
id: id,
name: name,
status: status
}) : false;
j ? this[Object.keys(this).filter(function (v) {
return v !== "id"
})[0]].push(JSON.parse(j)) : null;
return this
}.bind(data);
// set `id` at `data` ,
// add first item to `data.arrayname`
classData("01", "abc", "single", {"id":"01", "arrayname":"files"});
// set data at `data`
classData("02", "def", "married");
classData("02", "ghi", "single");
// read `classData`
console.log(classData());