• JavaScript(二)


    面向对象:

      JavaScript是面向对象的客户端脚本,可以使用基于面向对象的方式来控制数据,JavaScript的面向对象操作是基于prototype来实现的

      JS中function函数也是对象

    创建user对象的两种方式:

    方法一:

    var  User  =  function(){
      this.userName="tom";
      this.password="123";
    } User user
    = new User();

    方法二:

    var   user  =  new User();
    user.属性名 =  值;
    user.方法名 =  方法名

     prototype对User类型添加新的属性或方法:用于封装整块逻辑

      prototype是对类型进行扩展,不是对对象进行扩展,所以要写 User

    <script type="text/javascript">
        User.prototype.属性名 =  值;
        User.prototype.方法名  =  function(){}
    
        //例如
        //添加属性
        user.prototype.age=20;
        //添加方法
        User.prototype.getUserName  =  function(){
          return this.userName
        }
        User.prototype.setUserName  =  function(userName){
          this.userName=userName;
        }
    </script>

    JS中可以直接创建一个原生的Object对象

      里面没有任何方法,可以通过prototype添加,但是添加了以后所有的Object类型都会包括这种方法(不建议使用)

    <script type="text/javascript">
       var user=new Object();
      Object.prototype.userName="tom";
      alert(user.userName);
    </script>

    编写Employee类型
    具备empId,empName,salary属性
    1.要求对Employee进行实例化,访问所有的属性
    2.对Employee类型使用原型进行扩展,增加depId属性,并为该属性添加getter、setter方法

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
    
    <script type="text/javascript">
        //创建员工对象
        var Employee=function(){
            this.empId="1";
            this.empName="tom";
            this.salary="8000";
        }
        //对员工进行实例化
        var emp=new Employee();
        alert(emp);
        alert(emp.empId);
        alert(emp.empName);
        alert(emp.salary);
    
        //扩展属性
        Employee.prototype.depId="1001";
        //扩展方法
        Employee.prototype.getDepId=function(){
            return this.depId;
        }
        Employee.prototype.setDepId=function(depId){
            this.depId=depId
        }
    
        alert(emp.depId);
        var empId=emp.getDepId();
        alert(empId);
        emp.setDepId("2001");
        alert(emp.depId);
    </script>
    </body>
    </html>

    DOM编程

    DOM-Document Object Model ,它是W3C国际组织的一套Web标准,它定义了访问HTML文档对象的一套属性、方法和事件

  • 相关阅读:
    id4的数据库持久化写法
    docker 加速镜像的地址收集
    mongodb 的ID转换实体要注意的地方
    net core3.0 常用封装状态码总结
    JAVA8—————StringJoiner类
    BigDecimal加减乘除
    mysql 查询奇偶数
    Java遍历Map对象的方式
    Java中List, Integer[], int[]的相互转换
    springboot 读取resources下的文件然后下载
  • 原文地址:https://www.cnblogs.com/gfl-1112/p/12874872.html
Copyright © 2020-2023  润新知