[英]Is it possible to access to a class instance that it is inside another class in JavaScript?
[英]How to access methods inside another classes through instance of another class in Javascript
我有兩個類如下。
class Users{
createUser(){
//...
}
}
另一堂課
class Cars{
createCar(){
//....
}
}
主班
class Api{
//....
}
我需要訪問前兩個類雖然最后一個類如下
Api=new API()
Api.Users.createUser()
//also
Api.Cars.createCar()
在 javascript 中怎么可能。 這是一個好習慣嗎?
class Users { createUser(){ console.log('createUser'); } } class Cars { createCar(){ console.log('createCar') } } class Api { Users = new Users; Cars = new Cars; } var api = new Api() api.Users.createUser() api.Cars.createCar()
您需要在 API 類中創建每個類的實例。 然后你可以通過點符號訪問它們並調用它們各自的公共函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.