static - JavaScript | MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static

The static keyword defines a static method for a class. Static methods aren't called on instances of the class. Instead, they're called on the class itself. These are often utility functions, such as functions to create or clone objects.

Link to sectionSyntax

static methodName() { ... }

Link to sectionDescription

Static method calls are made directly on the class and are not callable on instances of the class. Static methods are often used to create utility functions.

Link to sectionCalling static methods

Link to sectionFrom another static method

In order to call a static method within another static method of the same class, you can use the this keyword.

class StaticMethodCall {
static staticMethod() {
return 'Static method has been called';
}
static anotherStaticMethod() {
return this.staticMethod() + ' from another static method';
}
}
StaticMethodCall.staticMethod();
// 'Static method has been called' StaticMethodCall.anotherStaticMethod();
// 'Static method has been called from another static method'

  

Link to sectionFrom class constructor and other methods

Static methods are not directly accessible using the this keyword from non-static methods. You need to call them using the class name: CLASSNAME.STATIC_METHOD_NAME() or by calling the method as a property of theconstructorthis.constructor.STATIC_METHOD_NAME().

class StaticMethodCall {
constructor() {
console.log(StaticMethodCall.staticMethod());
// 'static method has been called.' console.log(this.constructor.staticMethod());
// 'static method has been called.'
} static staticMethod() {
return 'static method has been called.';
}
}

  

Link to sectionExamples

The following example demonstrates several things:

  1. How a static method is implemented on a class.
  2. That a class with a static member can be sub-classed.
  3. How a static method can and cannot be called.
class Triple {
static triple(n) {
if (n === undefined) {
n = 1;
}
return n * 3;
}
} class BiggerTriple extends Triple {
static triple(n) {
return super.triple(n) * super.triple(n);
}
} console.log(Triple.triple()); // 3
console.log(Triple.triple(6)); // 18 var tp = new Triple(); console.log(BiggerTriple.triple(3));
// 81 (not affected by parent's instantiation) console.log(tp.triple());
// 'tp.triple is not a function'.

 

static 不被实例调用的更多相关文章

  1. Java基础(42):Java中主类中定义方法加static和不加static的区别(前者可以省略类名直接在主方法调用,后者必须先实例化后用实例调用)

    package lsg.ap.april4th2; /* 知识点:1.Getter and Setter 的应用 2.局部变量与成员变量(也可叫做全局变量) 3.Static关键字的用法 a.成员变量 ...

  2. 类实例调用静态方法(Java)

    前言 第一次看到在Java中是可以通过类实例调用静态方法,当然不推荐这么做,接下来会讲到,但是在C#中通过类实例调用静态方法在编译时就不会通过,这里做下记录. 类实例调用静态方法 首先我们来看一个简单 ...

  3. static成员函数不能调用non-static成员函数

    1 一般类静态成员函数不能调用非静态成员函数 2 static成员函数可以调用构造函数吗? 答案是肯定的,由于static成员函数没有this指针,所以一般static成员函数是不能访问non-sta ...

  4. static方法不能直接访问类内的非static变量和不能调用this,super语句分析

    大家都知道在static方法中,不能访问类内非static成员变量和方法.可是原因是什么呢? 这首先要从static方法的特性说起.static方法,即类的静态成员经常被称为"成员变量&qu ...

  5. Android实例-调用系统APP(XE10+小米2)

    相关资料:群号383675978 实例源码: unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, Sys ...

  6. 实例调用(__call__())

    任何类,只需要定义一个__call__()方法,就可直接对实例进行调用 对实例进行直接调用就好比对一个函数进行调用一样 __call__()还可定义参数,所以调用完全可以把对象看成函数,把函数看成对象 ...

  7. Python类和实例调用

    self指向的是实例对象,作为第一个参数,使用时不需要传入此参数. class Student(object): #定义一个Student类, def __init__(self, name, sco ...

  8. Python中模块、类、函数、实例调用案例

    19 a = '我是模块中的变量a' 20 21 def hi(): 22 a = '我是函数里的变量a' 23 print('函数"hi"已经运行!') 24 25 class ...

  9. SQL——存储过程实例 调用带参数的过程(成绩输出)

    create or replace procedure test_score(input in number,output out char) is begin then begin output : ...

随机推荐

  1. Codeforces 631B Print Check (思维)

    题目链接 Print Check 注意到行数加列数最大值只有几千,那么有效的操作数只有几千,那么把这些有效的操作求出来依次模拟就可以了. #include <bits/stdc++.h> ...

  2. 【IDEA】(3)---非常实用提高开发效率和水平的插件

    IDEA(3)-Mac中IDEA插件 IDEA提供了许多很实用的插件,能够大大提高开发效率和开发水平,这里列举几个很实用的插件. 说明:这边的IDEA是MAC系统. 一.插件管理界面简介 1.界面位置 ...

  3. php的session与免登陆问题

    Session 与 Session的GC 由于PHP的工作机制,它并没有一个daemon线程来定期的扫描Session 信息并判断其是否失效,当一个有效的请求发生时,PHP 会根据全局变量 sessi ...

  4. 苹果iOS APP配置HTTPS,iOS ATS配置SSL,苹果ATS标准解决方案

    参考沃通:

  5. IOS 暂停和恢复CALayer上的动画(转)

    coreAnimation的动画是存在于CALayer上面的,有些时候需要突然暂停某个组件的动画效果,同时保留当前动画的状态, 如果是用removeAnimation会显得很突兀,不够平滑,所以可以利 ...

  6. 数据挖掘-MovieLens数据集_电影推荐_亲和性分析_Aprioro算法

    #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Feb  7 14:38:33 201 ...

  7. Zookeeper demo增删改查

    Zookeeper 的增删改查demo代码 public class SimpleZkClient { private static final String connectString = &quo ...

  8. suid sgid sbit chattr lsattr find

    suid 一般用于二进制可执行文件不可用于shell脚本和目录,suid代表当用户执行此二进制文件时,暂时具有此文件所有者的权限 chmod 4xxx binfile sgid 一般用于目录,sgid ...

  9. 【Excle数据透视】如何创建多条件汇总的数据透视表

    创建多条件的数据透视表,源数据如下: 插入数据透视表 根据需求拖拽字段(统计该地区下销售人员销售业绩) 数据透视结果 数据透视表布局的各个区域说明 区域 说明 数值 用于显示汇总值数据 行标签 用于将 ...

  10. hdfs笔记

    Distributed File System 数据量越来越多,在一个操作系统管辖的范围存不下了,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,因此迫切需要一种系统来管理多台机器上的文 ...