static 不被实例调用
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:
- How a static method is implemented on a class.
- That a class with a static member can be sub-classed.
- 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 不被实例调用的更多相关文章
- Java基础(42):Java中主类中定义方法加static和不加static的区别(前者可以省略类名直接在主方法调用,后者必须先实例化后用实例调用)
package lsg.ap.april4th2; /* 知识点:1.Getter and Setter 的应用 2.局部变量与成员变量(也可叫做全局变量) 3.Static关键字的用法 a.成员变量 ...
- 类实例调用静态方法(Java)
前言 第一次看到在Java中是可以通过类实例调用静态方法,当然不推荐这么做,接下来会讲到,但是在C#中通过类实例调用静态方法在编译时就不会通过,这里做下记录. 类实例调用静态方法 首先我们来看一个简单 ...
- static成员函数不能调用non-static成员函数
1 一般类静态成员函数不能调用非静态成员函数 2 static成员函数可以调用构造函数吗? 答案是肯定的,由于static成员函数没有this指针,所以一般static成员函数是不能访问non-sta ...
- static方法不能直接访问类内的非static变量和不能调用this,super语句分析
大家都知道在static方法中,不能访问类内非static成员变量和方法.可是原因是什么呢? 这首先要从static方法的特性说起.static方法,即类的静态成员经常被称为"成员变量&qu ...
- Android实例-调用系统APP(XE10+小米2)
相关资料:群号383675978 实例源码: unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, Sys ...
- 实例调用(__call__())
任何类,只需要定义一个__call__()方法,就可直接对实例进行调用 对实例进行直接调用就好比对一个函数进行调用一样 __call__()还可定义参数,所以调用完全可以把对象看成函数,把函数看成对象 ...
- Python类和实例调用
self指向的是实例对象,作为第一个参数,使用时不需要传入此参数. class Student(object): #定义一个Student类, def __init__(self, name, sco ...
- Python中模块、类、函数、实例调用案例
19 a = '我是模块中的变量a' 20 21 def hi(): 22 a = '我是函数里的变量a' 23 print('函数"hi"已经运行!') 24 25 class ...
- SQL——存储过程实例 调用带参数的过程(成绩输出)
create or replace procedure test_score(input in number,output out char) is begin then begin output : ...
随机推荐
- Codeforces 371E Subway Innovation (前缀和预处理应用)
题目链接 Subway Innovation 首先不难想到所求的k个点一定是连续的,那么假设先选最前面的k个点,然后在O(1)内判断第2个点到第k+1个点这k个点哪个更优. 判断的时候用detla[i ...
- 浅谈.Net异步编程的前世今生----EAP篇
前言 在上一篇博文中,我们提到了APM模型实现异步编程的模式,通过使用APM模型,可以简化.Net中编写异步程序的方式,但APM模型本身依然存在一些缺点,如无法得知操作进度,不能取消异步操作等. 针对 ...
- Network | CIDR
无类别(现在) 无类别域间路由(Classless Inter-Domain Routing.CIDR)是一个用于给用户分配IP地址以及在互联网上有效地路由IP数据包的对IP地址进行归类的方法. CI ...
- ABS已死: Archlinux 放弃支持 ABS
今天访问archlinux官网,突然看到官方放弃支持ABS的新闻,声明如下: 由于 Arch Build System 的相关服务器端脚本的维护开销日益增高,我们决定放弃 abs及其相关的通过 rsy ...
- Tmux常用快捷键及命令
Exported from workflowy! tmux session start/create session- tmux- tmux new-session -s portage listin ...
- Java使用HttpURLConnection调用WebService(原始方法)
说明:使用Java原生的HttpURLConnection调用WebService可以免去引入SOA的框架,比如一些CXF框架等.可以使代码足够精简,比如对于一些只调用一两个接口的,这种方式是最适合的 ...
- Spring MVC的各种参数绑定方式(请求参数用基础类型和包装类型的区别)(转)
1.基本数据类型(以int为例,其他类似): Controller代码: @RequestMapping("saysth.do") public void test(int cou ...
- Java中获取当前时间并格式化
主要有两种方式,其中使用Date比较好控制,代码如下: //使用Calendar Calendar now = Calendar.getInstance(); System.out.println(& ...
- 巧用chrome开发者工具
说明:截图中的Chrome版本为52,不同版本可能略有区别. 常用设置 开发时消除静态资源缓存不能立刻更新的困扰,勾选Disable cache即可 切换颜色显示格式 修改默认颜色显示格式,在Sett ...
- linux中sed中用s 替换中遇到的问题
sed替换时候不能用-n,只输出替换的行,为什么???? 比如: sed "2,4s/\"//g" /var/lib/status #可以输出 但是: sed - ...