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 691E Xor-sequences(矩阵加速DP)
题目链接 Xor-sequences 利用矩阵加速. 先预处理出当序列长度为$2$的时候的方案数. 也就是说这个序列起点是$a[i]$终点是$a[j]$且中间没有任何元素. 但是所求的$k$很大,序列 ...
- 详解BitMap算法
所谓的BitMap就是用一个bit位来标记某个元素所对应的value,而key即是该元素,由于BitMap使用了bit位来存储数据,因此可以大大节省存储空间. 1. 基本思想 首先用一个简单的例子 ...
- 在typescript中import第三方类库clipboard报错
一.问题 在实际开发项目中就遇到了这样的问题,需要在Vue+Typescript项目中添加复制文本的功能,就找了clipboard插件,先是新建了一个新的项目用来实验看看是否好用,都写好了以后发给别人 ...
- java的架构流行阶段
第一阶段:SSM 第二阶段:分布式系统改造,平台化初具规模,各项垂直业务系统搭建上线.产品端极大丰富用户投资.大数据平台研究并使用 第三阶段:SOA治理,使用zookeeper作为注册中心,dubbo ...
- C#调用C++Dll封装时遇到的一系列问题【转】
最近帮底层开发的同时用C#重新封装一下dll,也就是用C#类来封装C++Dll里的方法,以供用户使用. 之前也用到过类似的应用,大多数问题都出在类型转换上,但是这次的应用层出不穷,所以在这里总结一 ...
- linux中grep注意
grep -l 只输出文件名: -h 只输出匹配的行 不输出文件名: -c 之处匹配内容的行数: -n 将结果输出的同时,也输出改行的行号: -c 统计查到的总行数: -i 忽略大小写: grep ' ...
- 移植opencv2.4.9到itop4412开发板
OpenCV是眼下开源项目中最著名的基于机器视觉方向的图像处理的开发包,眼下已经有被移植到嵌入式Linux环境上. 本文介绍了OpenCV交叉编译的基本步骤. 在opencv交叉编译之前要先进行依赖库 ...
- Mongodb性能调优
摘要 1. Mongodb 适用场景简介 2. Mongodb 性能监控与分析 3. Mongodb 性能优化建议 关于Mongodb的几个大事件 1.根据美国数据库知识大全官网发布的DB热度排行,M ...
- FreeMark的list应用
语法:<#if></#if>后台传送List,前台html页面中获取该list并显示: <#if userList?exists> <#list userLi ...
- Leetcode 编程训练(转载)
Leetcode这个网站上的题都是一些经典的公司用来面试应聘者的面试题,很多人通过刷这些题来应聘一些喜欢面试算法的公司,比如:Google.微软.Facebook.Amazon之类的这些公司,基本上是 ...