亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用此訪問靜態函數

使用此訪問靜態函數

富國滬深 2022-12-22 13:02:12
如果我有一個類:class Rectangle {  constructor(height, width) {    this.height = height;    this.width = width;  }  getArea() {    return Rectangle.area(this.height, this.width);  }  static area(height, width) {    return height * width;  }}然后我可以創建該類的實例并getArea在該實例上調用我的函數:var x = new Rectangle(5,6);console.log(x.getArea());現在,由于某種原因,在我的getArea函數中Rectangle.area,我不想直接調用,而是想動態地找到類,并在類實例的任何動態上調用靜態方法:  getArea() {    return this.class.area(this.height, this.width);  }static::area()在 PHP 中,我可以通過或之類的東西來做到這一點self::area()。我怎樣才能做一些類似于Javascriptstatic的事情self來動態訪問this所屬類的靜態方法?
查看完整描述

1 回答

?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

你可以參考x.constructor.area()。創建實例時,constructor將使其屬性引用構造函數。這就是找到靜態方法的地方。



查看完整回答
反對 回復 2022-12-22
  • 1 回答
  • 0 關注
  • 90 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號