[Javascript] Working with Static Properties on a Class

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Javascript] Working with Static Properties on a Class相关的知识,希望对你有一定的参考价值。

Classes are syntactic sugar over functions and functions are also referred to as "callable" objects. So it is possible to treat a function like an object and give them key / value properties like objects. The static keyword gives us the ability to assign a key / value property to a class itself, not an instance of that class. This lesson will walk you through using the static keyword and even show how to replicate it with regular functions.

 

class Retangle{
  static callRectangle(){
    return ‘hello world‘
  }
}

const myShape = new Rectangle() 
console.log(myShape.callRectangle) // error, you cannot call static prop on instance

 

But static prop can be called from child class:

function Rectangle(){
}

Rectangle.callRectangle = function(){
  return ‘hello world‘
}
class Square extends Rectangle {
  static whoAmI(){
    return "Hello, all " + super.callRectangle()
  }
}

console.log(Square.whoAmI()) //Hello, all hello world

 

以上是关于[Javascript] Working with Static Properties on a Class的主要内容,如果未能解决你的问题,请参考以下文章

extjs6学习笔记1.12 初始: Working with DOM

Working with Excel Files in Python

Chapter 1 Working with strings

Spring For Hadoop--Working With HBase

Working with forms

ImageSharp Working with images