• javascript类的定义及成员修改


    定义一个类

    //构造一个类 User

    var User=function(username){

    //加了this关键字 表示是实例变量  实例变量在类中使用必须带this.

     this.userName=username;

    //利用function定义的函数 和不加this定义的变量函数都是类变量

     function add(){

       alert(this.userName);

     }

    //变量函数

     minius=function(){

       alert("剪掉"+this.userName);

     }

    this.go=function(){

    alert("go"+this.userName);

    }

    类变量的修改 必须使用定义的类名

    User.prototype.add=function(){

    alert("用户名:"+this.userName);

    }

    User.prototype.minius=function(){

    alert("用户名:"+this.userName);

    }

    用了this关键字是不能用 类名去修改的


    比如 下面就是错误的

    User.prototype.go=function(){

    alert("用户名:"+this.userName);

    }

    用了this关键字必须用对象去改

    var user=new User("liaomin");

    user.go=function{

    alert("用户名:"+this.userName);

    }

    这样才是对的

    像java一样

    类变量和类函数(相当于java中的静态变量和静态函数) 是可以用对象.去修改 

    实例变量和函数 是不能用类.prototype去修改

  • 相关阅读:
    理解原型Prototype、继承
    解决js跨域问题的基本方法之 -------JSONP
    CSS3中动画效果Transitions与Animations的区别
    支付宝支付实例
    上线实例
    Celery
    Redis
    git
    jwt认证
    登录认证
  • 原文地址:https://www.cnblogs.com/liaomin416100569/p/9331540.html
Copyright © 2020-2023  润新知