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 theconstructor: this.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. HDFS Scribe Integration 【转】

    It is finally here: you can configure the open source log-aggregator, scribe, to log data directly i ...

  2. Unique Binary Search Trees II - LeetCode

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  3. 一次程序bug的排查

    这周准备下一个QA测试的版本,把版本发到测试环境就开始发现各种问题,修修补补搞了一周,总算告一段落了.   分析一下几个bug的问题,都集中在程序模块的整合中.一个模块的一个小的修改,造成另一个模块的 ...

  4. mfoc安装编译

    系统环境 UBUNTU16.04,要安装好各种基础的编译环境,这就不说了,如果不知道需要什么,那就编译时出错时看到什么安装什么吧 下载源码包libnfc, mfoc, mfcuk,都是github上, ...

  5. java 相关软件使用趋势

     http://www.baeldung.com/java-in-2017  https://mp.weixin.qq.com/s?__biz=MzI4NjYwMjcxOQ==&mid=224 ...

  6. 详解UIView的frame、bounds和center属性

    From: http://ios.wpjam.com/2011/08/29/uiview-frame-bounds-center/ 1.概要 翻开ios官方开发文档,赫然发现上面对这三个属性的解释如下 ...

  7. http://blog.csdn.net/LANGXINLEN/article/details/50421988

    GitHub上史上最全的Android开源项目分类汇总 今天在看博客的时候,无意中发现了 @Trinea在GitHub上的一个项目 Android开源项目分类汇总, 由于类容太多了,我没有一个个完整地 ...

  8. win10 只要打开文件对话框就卡死解决方法

    我电脑的问题是:win10系统,只要打开 文件对话框就卡死,假死,cpu100% 一直没有解决,但是只要把缩略图关了,就ok. 但是又想要留着缩略图,还是得显示,于是乎一直在找解决办法. 此方法好像可 ...

  9. Node.js 抓取电影天堂新上电影节目单及ftp链接

    代码地址如下:http://www.demodashi.com/demo/12368.html 1 概述 本实例主要使用Node.js去抓取电影的节目单,方便大家使用下载. 2 node packag ...

  10. TimeSpan时间间隔

    一个TimeSpan对象都表示一个时间间隔 (持续时间的时间或时间),单位为正数或负数的天数. 小时. 分钟. 秒和小数部分的第二个数字.TimeSpan结构还可以用于表示一天时间,但仅,如果与某一特 ...