当前位置: 移动技术网 > IT编程>开发语言>JavaScript > javascript当中的构造函数的用法

javascript当中的构造函数的用法

2020年01月11日  | 移动技术网IT编程  | 我要评论

5)构造函数的用法:

例 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>

文章转载自:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网