呵呵,俺看书上是这么写滴。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="Gao YiXiang" />
    <meta name="email" content="[email protected]" />
    <meta name="keywords" content="javascript dhtml dom" />
    <meta name="description" content="I love web development." />
</head>
<body>
    <script type="text/javascript">
    <!--
// 带参构造函数。
function SelectAttributes(id,session,name,date,command)
{
    this.id = ""; 
    this.session = ""; 
    this.name = ""; 
    this.date = ""; 
    this.command = "";     if (id)
        this.id = id;
    if (session)
        this.session = session;
    if (name)
        this.name = name;
    if (date)
        this.date = date;
    if (command)
        this.command = command;
}// 实例化。
var attr = new SelectAttributes("999","sessionValue","tom","2008-01-11","cmd");// 遍历属性。
for (var a in attr)
{
    alert(a + ": " + attr[a]);
}
    //-->
    </script>
</body>
</html>

解决方案 »

  1.   

    如果加个静态方法来实例化也可以。
    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="Gao YiXiang" />
        <meta name="email" content="[email protected]" />
        <meta name="keywords" content="javascript dhtml dom" />
        <meta name="description" content="I love web development." />
    </head>
    <body>
        <script type="text/javascript">
        <!--
    // 带参构造函数。
    function SelectAttributes(id,session,name,date,command)
    {
        this.id = ""; 
        this.session = ""; 
        this.name = ""; 
        this.date = ""; 
        this.command = "";     if (id)
            this.id = id;
        if (session)
            this.session = session;
        if (name)
            this.name = name;
        if (date)
            this.date = date;
        if (command)
            this.command = command;
    }SelectAttributes.getAttributes = function(id,session,name,date,command)
    {
        return new SelectAttributes(id,session,name,date,command);
    };// 实例化。
    //var attr = new SelectAttributes("999","sessionValue","tom","2008-01-11","cmd");
    var attr = SelectAttributes.getAttributes("999","sessionValue","tom","2008-01-11","cmd");// 遍历属性。
    for (var a in attr)
    {
        alert(a + ": " + attr[a]);
    }
        //-->
        </script>
    </body>
    </html>
      

  2.   

    谁有兴趣的话可以试一下,其实就是[object]和[object Object]的区别,或者说怎么把[object Object]变成[object]
      

  3.   

    搞什么啊~~
    [object]和[object   Object]根本没区别~有区别也只是toString()返回不同的字符串而已,typeof()返回的一样是object~~
    为什么是[object   Object]呢,我是这样么认为的~因为我们自定义的对象都不是顶级对象,至少也是window的子对象,所以toString()返回多个一个object,用以区分~仅此而已~~<script>
    SelectAttributes=function(){ 
    this.id=""; 
    this.session=""; 
    this.name=""; 
    this.date=""; 
    this.command=""; 
    }
    SelectAttributes.getAttributes   =   function(id,session,name,date,command){ 
    var   attributes=new   SelectAttributes(); 
    attributes.id=id; 
    attributes.session=session; 
    attributes.name=name; 
    attributes.date=date; 
    attributes.command=command; 
    return   attributes; 
    }
    </script>
    <input type="button" onclick="alert(typeof(new SelectAttributes()))" />
    <input type="button" onclick="alert(SelectAttributes.getAttributes('9527','','','','').id)" />
    我输出9527正常啊~
      

  4.   

    最好这么写function SelectAttributes(id,session,name,date,command){ 
        if(typeof(id)=="undefined"){
            this.id="";//最好是初始化为null
        }else{
            this.id=id;
        }
        //以下雷同
    }
      

  5.   

    也不行,参数传不出去。里面的属性值都是undefined
    ~~~~
    我也在测试没有问题,你的写法可以取到属性值,
      

  6.   

    对于到底是[object   Object]还是[object]没研究过,
    也没有听说过“正常的时候应该显示[[object]]”
      

  7.   

    呵呵,俺经常犯张冠李戴滴毛病!亲嘴儿就免了,哈---------------------------------
    除了这种以外我还试过直接在调用SelectAttributes的方法中直接建立一个SelectAttributes对象(也不行,参数传不出去。里面的属性值都是undefined)。 
    还试过建立一个全局的SelectAttributes对象(也不行) 
    ---------------------------------
    以上两种情况,lz滴代码是怎么写滴?!贴出来看看!
      

  8.   

    主题:请教关于[object Object]的含义
    http://www.javaeye.com/post/323319var s = {};   
    alert(s);//[object Object]
    打出的内容他的含义是什么呢,第一个object他是小写的o,第二个是大写的o,不理解他的意思?输出的s的toString的结果。按照惯例,第一个object表示它是一个对象(而不是值),后面表示它的类(构造函数名)。
    ----------------------------------------- 
    以上内容均为引用!这里只解释了[object Object],并未解释[object],俺正在思考中,哈
      

  9.   

    如果后面那个object是指构造函数名那就不对了啊,就从来没见过[object xxx]xxx不是Object的情况~~~
    我觉得应该是指他是window(或者是其它父对象的)子成员(构造函数)产生的~~后面那个Object只是代表它是一个引用类型~~
    你就当js为以后可以自定义struct类型留了一个关键字吧~~
      

  10.   

    就从来没见过[object xxx]
    -------------------------
    这是因为很少有人重写toString()方法,哈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="Gao YiXiang" />
        <meta name="email" content="[email protected]" />
        <meta name="keywords" content="javascript dhtml dom" />
        <meta name="description" content="I love web development." />
    </head>
    <body>
        <script type="text/javascript">
        <!--function Robot() 
    {
       this.hasJetpack = true;   this.toString = function()
       {
           return "[object Robot]";
       };
    }var r = new Robot();
    alert(r); // [object Robot]    //-->
        </script>
    </body>
    </html>
      

  11.   

    以下是俺做滴一个小试验及其结果,大家一起来 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>js.basedOnObject.html</title>
        <meta name="generator" content="editplus" />
        <meta name="author" content="Gao YiXiang" />
        <meta name="email" content="[email protected]" />
        <meta name="keywords" content="javascript dhtml dom" />
        <meta name="description" content="I love web development." />
        <style type="text/css">
    body
    {
        font-family: Courier New;
        font-size: 15px;
        line-height: 20px;
    }
    pre
    {
        font-family: Courier New;
        font-size: 15px;
        color: red;
    }
        </style>
    </head>
    <body>
        <script type="text/javascript">
        <!--function Robot() 
    {
        this.hasJetpack = true;    // 重写 toString() 方法。
        this.toString = function()
        {
           return "[object Robot]";
        };
    }var e = document.createElement("a");
    var o = new Object();
    var r = new Robot();var wrap = "<br />";
    var output = "";output += "---- ---- HTML A 元素 ---- ----" + wrap;
    output += "e: " + e + wrap;
    output += "toString(): " + e.toString() + wrap;
    output += "toString: " + e.toString + wrap;
    output += "typeof toString: " + (typeof e.toString) + wrap;
    output += "toString instanceof Object: " + (e.toString instanceof Object) + wrap;
    output += "toString.constructor: " + e.toString.constructor + wrap;
    output += "typeof: " + (typeof e) + wrap;
    output += "instanceof Object: " + (e instanceof Object) + wrap;
    output += "constructor: " + "<pre>" + e.constructor + "</pre>" + wrap;output += "---- ---- Object 对象实例 ---- ----" + wrap;
    output += "o: " + o + wrap;
    output += "toString(): " + o.toString() + wrap;
    output += "toString: " + o.toString + wrap;
    output += "typeof toString: " + (typeof o.toString) + wrap;
    output += "toString instanceof Object: " + (o.toString instanceof Object) + wrap;
    output += "toString.constructor: " + o.toString.constructor + wrap;
    output += "typeof: " + (typeof o) + wrap;
    output += "instanceof Object: " + (o instanceof Object) + wrap;
    output += "constructor: " + "<pre>" + o.constructor + "</pre>" + wrap;output += "---- ---- 自定义 Robot 对象实例 ---- ----" + wrap;
    output += "r: " + r + wrap;
    output += "toString(): " + r.toString() + wrap;
    output += "toString: " + r.toString + wrap;
    output += "typeof toString: " + (typeof r.toString) + wrap;
    output += "toString instanceof Object: " + (r.toString instanceof Object) + wrap;
    output += "toString.constructor: " + r.toString.constructor + wrap;
    output += "typeof: " + (typeof r) + wrap;
    output += "instanceof Object: " + (r instanceof Object) + wrap;
    output += "constructor: " + "<pre>" + r.constructor + "</pre>" + wrap;document.write(output);/*
    typeof 
    The typeof operator returns a string indicating the data type of its operand.
    typeof 仅通过一个字符串表明操作数的数据类型(boolean, number, string, object, function, undefined)。instanceof 
    Evaluates true if the first operand is an instance of the second operand. 
    The second operand must be an object (for example, a constructor).
    instanceof 用来评估第一操作数是否为第二操作数滴实例。
    第二操作数必须是一个对象(例如,一个构造函数)。Object (Built-in Object)
    Methods
    toString() Returns the object a string, by default "[object Object]". Very often overridden 
    to provide specific functionality.
    可见所有用户自定类的实例,如果没有重写toString滴话,都会调用Object原型中的toString方法,而返回[object Object],
    因为它们都是从内建对象Object继承的。
    注意:Array重写了toString方法。
    上面例子中滴Robot也重写了toString方法。Constructors
    Object instances are created with constructors, which are basically special functions that 
    prepare new instances of an object for use. Every constructor contains an object prototype 
    that defines the code and data that each object instance has by default.
    构造器是用来创建对象实例的特定函数。每个构造器包含一个对象原型。可见如果 myInstance instanceof Object 为 true,那么myInstance.constructor一定会返回其构造器;
    若为 false,那么返回很可能就是 undefined,见试验结果。*//* 输出结果:
    ---- ---- HTML A 元素 ---- ----
    e: 
    toString(): 
    toString: function toString() { [native code] } 
    typeof toString: object
    toString instanceof Object: false
    toString.constructor: undefined
    typeof: object
    instanceof Object: false
    constructor: 
    undefined
    ---- ---- Object 对象实例 ---- ----
    o: [object Object]
    toString(): [object Object]
    toString: function toString() { [native code] } 
    typeof toString: function
    toString instanceof Object: true
    toString.constructor: function Function() { [native code] } 
    typeof: object
    instanceof Object: true
    constructor: 
    function Object() {
        [native code]
    }---- ---- 自定义 Robot 对象实例 ---- ----
    r: [object Robot]
    toString(): [object Robot]
    toString: function() { return "[object Robot]"; }
    typeof toString: function
    toString instanceof Object: true
    toString.constructor: function Function() { [native code] } 
    typeof: object
    instanceof Object: true
    constructor: 
    function Robot() 
    {
        this.hasJetpack = true;    // 重写 toString() 方法。
        this.toString = function()
        {
           return "[object Robot]";
        };
    }
    */
        //-->
        </script>
    </body>
    </html>
      

  12.   

    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head>
    <body><script language="javascript"> 
    SelectAttributes = function(id,session,name,date,command){ 
    this.id=id; 
    this.session=session; 
    this.name=name; 
    this.date=date; 
    this.command=command;
    }function a(o){
    alert(o.session);
    o.command();
    }</script><input name="" type="button" value="111" onclick="a(new SelectAttributes(1,2,3,4,function(){alert(5)}));"/>
    <input name="" type="button" value="222" onclick="a(new SelectAttributes(6,7,8,9,function(){alert(0)}));"/>
    </body>
    </html>lz要这个?
      

  13.   

    由于俺临时把document.createElement("input")改成了document.createElement("a"),
    从而导致结果发生了一些有趣滴变化,现在先改回来,一会儿再讨论input和a滴差别。<!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>js.basedOnObject.html</title>
        <meta name="generator" content="editplus" />
        <meta name="author" content="Gao YiXiang" />
        <meta name="email" content="[email protected]" />
        <meta name="keywords" content="javascript dhtml dom" />
        <meta name="description" content="I love web development." />
        <style type="text/css">
    body
    {
        font-family: Courier New;
        font-size: 15px;
        line-height: 20px;
    }
    pre
    {
        font-family: Courier New;
        font-size: 15px;
        color: red;
    }
        </style>
    </head>
    <body>
        <script type="text/javascript">
        <!--function Robot() 
    {
        this.hasJetpack = true;    // 重写 toString() 方法。
        this.toString = function()
        {
           return "[object Robot]";
        };
    }var e = document.createElement("input");
    var o = new Object();
    var r = new Robot();var wrap = "<br />";
    var output = "";output += "---- ---- HTML INPUT 元素 ---- ----" + wrap;
    output += "e: " + e + wrap;
    output += "toString(): " + e.toString() + wrap;
    output += "toString: " + e.toString + wrap;
    output += "typeof toString: " + (typeof e.toString) + wrap;
    output += "toString instanceof Object: " + (e.toString instanceof Object) + wrap;
    output += "toString.constructor: " + e.toString.constructor + wrap;
    output += "typeof: " + (typeof e) + wrap;
    output += "instanceof Object: " + (e instanceof Object) + wrap;
    output += "constructor: " + "<pre>" + e.constructor + "</pre>" + wrap;output += "---- ---- Object 对象实例 ---- ----" + wrap;
    output += "o: " + o + wrap;
    output += "toString(): " + o.toString() + wrap;
    output += "toString: " + o.toString + wrap;
    output += "typeof toString: " + (typeof o.toString) + wrap;
    output += "toString instanceof Object: " + (o.toString instanceof Object) + wrap;
    output += "toString.constructor: " + o.toString.constructor + wrap;
    output += "typeof: " + (typeof o) + wrap;
    output += "instanceof Object: " + (o instanceof Object) + wrap;
    output += "constructor: " + "<pre>" + o.constructor + "</pre>" + wrap;output += "---- ---- 自定义 Robot 对象实例 ---- ----" + wrap;
    output += "r: " + r + wrap;
    output += "toString(): " + r.toString() + wrap;
    output += "toString: " + r.toString + wrap;
    output += "typeof toString: " + (typeof r.toString) + wrap;
    output += "toString instanceof Object: " + (r.toString instanceof Object) + wrap;
    output += "toString.constructor: " + r.toString.constructor + wrap;
    output += "typeof: " + (typeof r) + wrap;
    output += "instanceof Object: " + (r instanceof Object) + wrap;
    output += "constructor: " + "<pre>" + r.constructor + "</pre>" + wrap;document.write(output);/*
    typeof 
    The typeof operator returns a string indicating the data type of its operand.
    typeof 仅通过一个字符串表明操作数的数据类型(boolean, number, string, object, function, undefined)。instanceof 
    Evaluates true if the first operand is an instance of the second operand. 
    The second operand must be an object (for example, a constructor).
    instanceof 用来评估第一操作数是否为第二操作数滴实例。
    第二操作数必须是一个对象(例如,一个构造函数)。Object (Built-in Object)
    Methods
    toString() Returns the object a string, by default "[object Object]". Very often overridden 
    to provide specific functionality.
    可见所有用户自定类的实例,如果没有重写toString滴话,都会调用Object原型中的toString方法,而返回[object Object],
    因为它们都是从内建对象Object继承的。
    注意:Array重写了toString方法。
    上面例子中滴Robot也重写了toString方法。Constructors
    Object instances are created with constructors, which are basically special functions that 
    prepare new instances of an object for use. Every constructor contains an object prototype 
    that defines the code and data that each object instance has by default.
    构造器是用来创建对象实例的特定函数。每个构造器包含一个对象原型。可见如果 myInstance instanceof Object 为 true,那么myInstance.constructor一定会返回其构造器;
    若为 false,那么返回很可能就是 undefined,见试验结果。*//* 输出结果:
    ---- ---- HTML INPUT 元素 ---- ----
    e: [object]
    toString(): [object]
    toString: function toString() { [native code] } 
    typeof toString: object
    toString instanceof Object: false
    toString.constructor: undefined
    typeof: object
    instanceof Object: false
    constructor: 
    undefined
    ---- ---- Object 对象实例 ---- ----
    o: [object Object]
    toString(): [object Object]
    toString: function toString() { [native code] } 
    typeof toString: function
    toString instanceof Object: true
    toString.constructor: function Function() { [native code] } 
    typeof: object
    instanceof Object: true
    constructor: 
    function Object() {
        [native code]
    }---- ---- 自定义 Robot 对象实例 ---- ----
    r: [object Robot]
    toString(): [object Robot]
    toString: function() { return "[object Robot]"; }
    typeof toString: function
    toString instanceof Object: true
    toString.constructor: function Function() { [native code] } 
    typeof: object
    instanceof Object: true
    constructor: 
    function Robot() 
    {
        this.hasJetpack = true;    // 重写 toString() 方法。
        this.toString = function()
        {
           return "[object Robot]";
        };
    }
    */
        //-->
        </script>
    </body>
    </html>