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
  • 相关阅读:
    常用数列
    sqrt
    树状数组
    hash
    P1102 A-B数对
    codevs 1795 金字塔 2
    P2296 寻找道路
    [USACO16JAN]子共七Subsequences Summing to Sevens
    P3397 地毯
    关于调用&&传址
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12003897.html
Copyright © 2011-2022 走看看