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>

文章转载自:https://blog.csdn.net/qq_44594249/article/details/100032253

javascript当中的构造函数的用法的更多相关文章

  1. javascript --- 设计模式之构造函数模式

    在JavaScript里,构造函数通常是认为用来实现实例的,JavaScript没有类的概念,但是有特殊的构造函数.通过new关键字来调用定义的否早函数,你可以告诉JavaScript你要创建一个新对 ...

  2. JavaScript设计模式之构造函数模式

    一.构造函数模式概念 构造函数用于创建特定类型的对象——不仅声明了使用过的对象,构造函数还可以接受参数以便第一次创建对象的时候设置对象的成员值.你可以自定义自己的构造函数,然后在里面声明自定义类型对象 ...

  3. 研究 Javascript的&&和||的另类用法

    这篇文章主要介绍了Javascript的&&和||的另类用法,需要的朋友可以参考下 最近也没什么心思写文章了,感觉总有忙不完的事情,呵. 不过这些天又开始研究起 Titanium 来, ...

  4. C# 当中 LINQ 的常规用法(Lambda 方式)

    仅以本篇博文记录 LINQ 相关操作的基本知识,原型参考自 MSDN 相关知识,中间加以自己的理解与 DEMO. 1. IEnuemrable<T>.Select() Select 方法比 ...

  5. Javascript的&&和||的另类用法

    Javascript的&&和||的另类用法. 又开始研究起 Titanium 来,发现其官方出品的MVC框架(Alloy)还是挺不错的,刚开始苦于没有好的代码来学习,文档又少,所以一直 ...

  6. 详解 javascript中offsetleft属性的用法(转)

    详解 javascript中offsetleft属性的用法 转载  2015-11-11   投稿:mrr    我要评论 本章节通过代码实例介绍一下offsetleft属性的用法,需要的朋友可以做一 ...

  7. JavaScript中的构造函数和工厂函数说明

    在cnblog上看到一篇文章,讲解JS中的构造函数和工厂函数,觉得讲的真好 JavaScript中的工厂函数和构造函数都能用来创建一个对象,我们可以来看看下面的例子 构造函数 function cre ...

  8. Javascript自定义事件功能与用法实例分析

    原文地址:https://www.jb51.net/article/127776.htm 本文实例讲述了javascript自定义事件功能与用法.分享给大家供大家参考,具体如下: 概述 自定义事件很难 ...

  9. JavaScript中的构造函数和原型!

    JavaScript中的原型! 原型的内容是涉及到JavaScript中的构造函数的 每一个构造函数都有一个原型对象!prototype 他的作用是 共享方法!还可以扩展内置对象[对原来的内置对象进行 ...

随机推荐

  1. warning: LF will be replaced by CRLF in

    warning: LF will be replaced by CRLF in analysis/Result.csv. The file will have its original line en ...

  2. Debian 10 xfce 错误提示 ACCESS DENIED

    闲来无事重新安装自己的服务器发现很多关于Debian的初始安装问题都已经陌生了在此重新整理下自己安装所遇到的问题: ACCESS DENIED  释: 登录成功拒绝Root密码访问 解决方法: loc ...

  3. 常用 SQL Server 脚本

    运行维护 备份还原 # server_name # user_name # password # database_name # backup_path,备份路径,X:PathToBackupLoca ...

  4. P5443 [APIO2019]桥梁 [分块+并查集]

    分块+并查集,大板子,没了. 并查集不路径压缩,可撤销,然后暴力删除 这样对于每个块都是独立的,所以直接搞就行了. 然后块内修改操作搞掉,就是单独的了 // powered by c++11 // b ...

  5. Python安装和配置环境变量(简明教程)

    声明:借鉴Python 简明教程 安装我们在本书中提到的「Python 3」指的是 Python 版本大于或等于 Python 3.6.0. 针对Python3.6.版本:注意数据的缓存机制 # ## ...

  6. [转]Maven的pom.xml文件详解

    Maven的pom.xml文件详解------Build Settings 2013年10月30日 13:04:01 阅读数:44678 根据POM 4.0.0 XSD,build元素概念性的划分为两 ...

  7. java文本文件加密

    加密方法是通过输入流对源文件字符逐个读取,对其读取到字符的ascll值进行异或运算,并将其放入新文件中,解密时只要用相同的密钥进行ascll异或运算并向新文件输出即可,即对文件首次用该程序处理为加密, ...

  8. day6 基础总结和编码方式

    # = 赋值 == 比较值是否相等 is 比较内存地址 li1 = [1, 2, 3] li2 = li1 print(li1 is li2) print(id(li1), id(li2)) #数字, ...

  9. 杜教筛BM

    #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> # ...

  10. commons-dbutils实现增删改查(spring新注解)

    1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...