zoukankan      html  css  js  c++  java
  • JS 中的静态方法

    原生JS(es5)中的静态方法:

    //原生JS中的静态方法
    
    function Person(name, age) {
        this.name = name;
        this.age = age;
        this.run = function () {
            console.log(`${this.name} is ${this.age}岁`)
        }
    }
    
    Person.prototype.sex = '男'
    Person.prototype.work = function () {
        console.log(`${this.name} is ${this.sex} and ${this.age}岁`)
    }
    
    //静态方法
    Person.fun = function () {
        console.log("fun 静态方法")
    }
    
    var p = new Person('jack', 20) //实例方法是通过实例化来调用的
    p.run()
    p.work()
    Person.fun() //静态方法调用,直接通过类名调用
    
    /**
     * jack is 20岁
       jack is 男 and 20岁
       fun 静态方法
     */

    ES6 中的静态方法:

    通过 static 关键字

    //es6里面的静态方法
    class Person {
        constructor(name) {
            this._name = name;  //属性
        }
        run() {  //实例方法
            console.log("实例方法",this._name);
        }
        static work() {   //静态方法
            console.log('work 静态方法');
        }
    }
    
    Person.value = '这是一个静态方法的属性';
    
    var p = new Person('jack');
    p.run();
    Person.work();   //es6里面的静态方法执行
    
    console.log(Person.value);
    
    /**
     * 实例方法 jack
       work 静态方法
       这是一个静态方法的属性
     */
    

      

  • 相关阅读:
    loadrunner监控linux之linux下安装rpc
    Linux中top命令参数详解
    使用jmeter监控服务器性能指标
    jmeter连接mysql数据库配置
    loadrunner--设置代理录制
    页面下载时间细分图组成
    linux网络配置
    科学把妹法
    简单而强大的bitset
    名言札记
  • 原文地址:https://www.cnblogs.com/shanlu0000/p/13172474.html
Copyright © 2011-2022 走看看