js class static property

class static property (public class fields)

const log = console.log;

class ESNextStaticProperty {
static username = "xgqfrms";
} log(ESNextStaticProperty.username);
// "xgqfrms" ESNextStaticProperty.name;
// "ESNextStaticProperty"

equal to


const log = console.log; class ES6StaticProperty {
// static name = "xgqfrms";
} ES6StaticProperty.username = "xgqfrms"; log(ESNextStaticProperty.username);
// "xgqfrms" ES6StaticProperty.name;
// "ES6StaticProperty"

https://javascript.info/static-properties-methods#static-properties

public class fields

class ClassWithInstanceField {
instanceField = 'instance field'
} class ClassWithStaticField {
static staticField = 'static field'
} class ClassWithPublicInstanceMethod {
publicMethod() {
return 'hello world'
}
}

private class fields


class ClassWithPrivateField {
#privateField
} class ClassWithPrivateMethod {
#privateMethod() {
return 'hello world'
}
} class ClassWithPrivateStaticField {
static #PRIVATE_STATIC_FIELD
}

demos

static properties

const log = console.log;

class ClassWithStaticProperty {
static staticProperty = `this is a static property `;
} const test = ClassWithStaticProperty.staticProperty;
log(test);
// "this is a static property"

static methods

const log = console.log;

class ClassWithStaticMethod {
static staticMethod() {
log(`static method has been called`);
return `static method`;
}
} const test = ClassWithStaticMethod.staticMethod();
// "static method has been called"
log(test);
// "static method"

web components

class EmojiElement extends HTMLElement {
constructor() {
super();
this.shadow = this.shadowRoot;
// ... web components
const template = document.querySelect(`[data-template="emoji-template"]`);
this.templateContent = template.content.cloneNode(true);
this.shadowEmoji.appendChild(this.templateContent);
// 添加样式到 Shadow DOM (template)
if (window.ShadyCSS) {
window.ShadyCSS.prepareTemplate(template, EmojiElement.tagName);
}
}
static tagName = `emoji-element`;
connect() {
log(`connect`);
}
disconnect() {
log(`disconnect`);
}
} customElements.define(EmojiElement.tagName, EmojiElement); Emoji.tagName;
// "emoji-element"

refs

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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Public_class_fields

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#Static_methods

https://javascript.info/static-properties-methods#static-properties

http://thecodebarbarian.com/static-properties-in-javascript-with-inheritance.html

https://www.sitepoint.com/javascript-private-class-fields/

https://medium.com/@assortedPickle/es6-static-properties-b7fd2a163328



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


js class static property & public class fields & private class fields的更多相关文章

  1. 浅析java修饰符之public default protected private static final abstract

    浅析java修饰符之public default protected private static final abstract 一   修饰符的作用:用来定义类.方法或者变量,通常放在语句的最前端 ...

  2. 详解Java中的访问控制修饰符(public, protected, default, private)

    Java中的访问控制修饰符已经困惑笔者多时,其中较复杂的情况一直不能理解透彻.今天下定决心,系统.全面地研究Java中的访问控制修饰符的所有方面,并整理成这篇文章,希望有同样疑惑的读者读完后能有所收获 ...

  3. 已知有一个Worker 类如下:  public class Worker  { private int age;  private String name;  private double salary;  public Worker (){}  public Worker (String nam

    package homework006; public class Worker { private int age; private String name; private double sala ...

  4. java 修饰符的作用一(public protected default private 组)

    1.public protected default private 组 public 权限最大,同类,同包,不同包,同包子类父类之间,不同包子类父类之间都可以访问. java 默认的权限是defau ...

  5. Java访问权限修饰符public protected friendly private用法总结(转载好文Mark)

    首先声明:Java中,friendly这个修饰符并没有显式的声明,在成员变量和方法前什么修饰符也不用,默认的就是friendly.为了条理清晰,分三种不同情况来总结. 一 访问权限修饰符修饰成员变量和 ...

  6. c# 单元测试 ,对静态方法(static)和私有方法(private) 进行单元测试

    利用反射: /// <summary> /// 调用静态方法 /// </summary>akf /// <param name="t">类全名 ...

  7. Azure PowerShell (9) 使用PowerShell导出订阅下所有的Azure VM的Public IP和Private IP

    <Windows Azure Platform 系列文章目录> 笔者在之前的工作中,有客户提出想一次性查看Azure订阅下的所有Azure VM的Public IP和Private IP. ...

  8. public,protected,friendly,private的访问权限

    请说出作用域public,private,protected,以及不写时的区别 这四个作用域的可见范围如下表所示. 说明:如果在修饰的元素上面没有写任何访问修饰符,则表示friendly. 作用域   ...

  9. java public protect default private

    (1)对于public修饰符,它具有最大的访问权限,可以访问任何一个在CLASSPATH下的类.接口.异常等.它往往用于对外的情况,也就是对象或类对外的一种接口的形式. (2)对于protected修 ...

随机推荐

  1. 类型检查和鸭子类型 Duck typing in computer programming is an application of the duck test 鸭子测试 鸭子类型 指示编译器将类的类型检查安排在运行时而不是编译时 type checking can be specified to occur at run time rather than compile time.

    Go所提供的面向对象功能十分简洁,但却兼具了类型检查和鸭子类型两者的有点,这是何等优秀的设计啊! Duck typing in computer programming is an applicati ...

  2. 内存模型 Memory model 内存分布及程序运行中(BSS段、数据段、代码段、堆栈

    C语言中内存分布及程序运行中(BSS段.数据段.代码段.堆栈) - 秦宝艳的个人页面 - 开源中国 https://my.oschina.net/pollybl1255/blog/140323 Mem ...

  3. CF460C Present

    写在前面 由于菜,写树状数组写挂了. 于是想出了一种不像线段树或树状数组+二分答案那样显然,但是依旧不难想,复杂度比较优秀,代码难度低的做法. 算法思路 外部二分答案,不多解释,稍证明一下单调性: 若 ...

  4. Spring5源码,@Autowired

    一.@Autowired所具有的功能 二.在Spring中如何使用@Autowired 三.@Autowired注解背后的工作原理 一.@Autowired所具有的功能 @Autowired是一个用来 ...

  5. java架构《并发线程基础二》

    1.关键字 volatile            使用场景: 针对与多线程公用的数据  用volatile关键字修饰  但其不保证原子性(同步).volatile关键字不具备synchronized ...

  6. C/C++ ===复习==函数返回值问题(集合体==网络)

    按值传递 地址传递: 应该明白只有这2种传递,下面讨论函数的按值传递 #include <stdio.h> #include <stdlib.h> int add_rtVal( ...

  7. 2.centos 7清空文件和文件夹

    1.清空文件 测试文件:a.txt 1)方法一,[root@centos test]# > a.txt [root@centos test]# cat a.txt 1hjbfao hjkl23o ...

  8. (31)grep命令详解:查找文件内容

    1.grep命令用于不需要列出文件的全部内容,而是从文件中找到包含指定信息的那些行. grep命令能够在一个或多个文件中,搜索某一特定的字符模式(也就是正则表达式),此模式可以是单一的字符.字符串.单 ...

  9. Linux Bash编程

    在Linux系统介绍中,介绍了shell的多个版本,现在的Linux发行版基本都默认使用bash(Bourne Again shell),兼容Bourne shell (sh),本文将简要介绍Bash ...

  10. Jenkins(4)docker容器内部修改jenkins容器时间

    前言 用docker搭建的Jenkins环境时间显示和我们本地时间相差8个小时,需修改容器内部的系统时间 查看时间 查看系统时间 date-R 进入docker容器内部,查看容器时间 docker e ...