想定义一个对象,其中有个属性是集合属性。不知如何定义。请指教!如:  任务列表var TaskList=function()
{
 this.name="aaa";
 this.descript="aaaaa";
//请问这个属性里有若干个对象应该如何定义!
 this.node={task1,task2.....}
//
}

解决方案 »

  1.   

    this.node=["task1","task2".....]
      

  2.   

    JS 中虽然有集合的概念,但是无法直接声明一个集合的实例,只能使用数组(Array)来代替,#1 已给出数组的简易定义形式。
      

  3.   

    这两个概念尽量不要混淆起来,分开说更清楚一些,
    Array 是 JavaScript 中的内建对象;
    而 JScript 中有 collection 的概念,通常会有 item 方法和 length 属性:L@_@K
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title> new document </title>
        <meta name="generator" content="editplus" />
        <meta name="author" content="[email protected]" />
        <meta name="keywords" content="" />
        <meta name="description" content="" />
    </head>
    <body>
    <script type="text/javascript">
    <!--
    function WriteLine(contents) {
        document.write(contents + "<br />");
    }// Retrieves an object from the all collection or various other collections.
    var allElements = document.body.all;WriteLine(typeof(allElements)); // object
    WriteLine(allElements.constructor); // undefined
    WriteLine(allElements instanceof Array); // false
    WriteLine(allElements.length); // 4
    //-->
    </script>
    </body>
    </html>
    Web 开发常用手册DHTML 参考手册
    http://download.csdn.net/source/308913JScript 语言参考
    http://download.csdn.net/source/308916CCS 样式表中文手册
    http://download.csdn.net/source/304124
      

  4.   

    如果把 #4 的例子换做 ArrayL@_@K
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title> new document </title>
        <meta name="generator" content="editplus" />
        <meta name="author" content="[email protected]" />
        <meta name="keywords" content="" />
        <meta name="description" content="" />
    </head>
    <body>
    <script type="text/javascript">
    <!--
    function WriteLine(contents) {
        document.write(contents + "<br />");
    }// 声明数组实例。
    var allElements = ["node1", "node2", "node3", "node4"];WriteLine(typeof(allElements)); // object
    WriteLine(allElements.constructor); // function Array() { [native code] }
    WriteLine(allElements instanceof Array); // true
    WriteLine(allElements.length); // 4
    //-->
    </script>
    </body>
    </html>
      

  5.   

    在下才疏学浅,没有看明白,我的子属性中不知道有多少个?我怎么定义呀?
    类似这样的:
    var allElements = ["node1", "node2", "node3", "node4"];
    好像已经定义好了!
      

  6.   

    如果还用数组的话,可以这么写:
        var allElements = ["node1", "node2", "node3", "node4"];
        allElements.push("node5");
        allElements.push("node6");
        alert(allElements.length); // 6Web 开发常用手册DHTML 参考手册
    http://download.csdn.net/source/308913JScript 语言参考
    http://download.csdn.net/source/308916CCS 样式表中文手册
    http://download.csdn.net/source/304124