zoukankan      html  css  js  c++  java
  • [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
  • 相关阅读:
    Acdream 1174 Sum 暴力
    Acdream 1114 Number theory 莫比乌斯反演
    Acdream 1007 快速幂,模乘法
    UVa 10023
    UVa 11027
    UVa 11029
    UVa 10820
    UVa 10791
    UVa 11121
    UVa 106
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12003897.html
Copyright © 2011-2022 走看看