javascript当中的构造函数的用法

构造函数的用法:
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。



例 3.5.1

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<script>
    function Student(name, age)
    {
        /* 马克-to-win:later on we can use it in
        var doc = new ActiveXObject( "Microsoft.XMLDOM" );
                    doc.async="false";
                    doc.load(str);
        when a property has a this, means that this property is a member property.
        */
        this.name = name;
        this.age = age;
        this.parti = function()
        {
            document.writeln("名字是:" + this.name + "<br>");
            document.writeln("年纪是:" + this.age + "<br>");
        };
    }
    var p = new Student('jeri', 3);
    document.writeln("typeof p is " + typeof(p));
    //typeof(p) is object
    p.parti();
    p.age = 4;
    p.parti();
    /*the following two methods can also access some properties.*/
    document.writeln("" + p["age"]);
    document.writeln("" + p["a" + "ge"]);


    if (p instanceof Student) document.writeln("p是Student的实例<br>");
    /*javascript 中的对象全部是Object 的子类
    Because this object is the topmost parent object in the prototype inheritance hierarchy, all other object classes inherit its methods and properties. It's a close enough call that JavaScript 2.0 may well move it into the class-based object-oriented category at which time the prototype inheritance would be replaced with super-class/sub-class mechanisms and the arguments become null and void.  */
    /*When the Global object is created, it always has at least the following properties:
       Object object
       Function object
       Array object
       String object
       Boolean object
       Number object
       Date object
       Math object
       Value properties
   */
    if (p instanceof Object) document.writeln("p是Object的实例");
</script>


输出结果:
typeof p is object 名字是:jeri
年纪是:3
名字是:jeri
年纪是:4
4 4 p是Student的实例
p是Object的实例




例 3.5.2

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<script>
    function f2()
    {
        this.lll = "马克-to-win";
        document.writeln("in f2<br>");
        /*for new operator, the following statement got no use. */
        return 'Hello 马克-to-win';
    }
    /*so f2 has dual role, one is ordinary function, another is that construactor, see whether you use new operator or just simply call it.*/
    var f5 = f2();
    document.writeln("typeof f5是:" + typeof f5 + "qqq" + typeof f2 + "qqqq" + f5 + "<br>");
    document.writeln("f5.lll是:" + f5.lll + "<br>"+window.lll+lll);
    var f6 = new f2();
    document.writeln("typeof f6是:" + typeof f6 + "qqq" + typeof f2 + "qqqq" + f6 + "<br>");
    document.writeln("f6.lll是:" + f6.lll + "<br>"+window.lll+lll);
  
    var zhanwei = "zhanwei";
</script>



输出结果:
in f2
typeof f5是:stringqqqfunctionqqqqHello 马克-to-win
f5.lll是:undefined
马克-to-win马克-to-win in f2
typeof f6是:objectqqqfunctionqqqq[object Object]
f6.lll是:马克-to-win
马克-to-win马克-to-win