我在使用Google Firebase身份验证和Firebase Firestore时遇到了这个问题。您可以使用相同的名称访问变量及其类的功能,例如:
//To authenticate:
firebase.auth().signInWithEmailAndPassword()
//Or to access a value
firebase.auth.Auth.Persistence.SESSION
注意它们如何同时使用firebase.auth
和firebase.auth()
。当我尝试查看是否可以执行此操作时,它将引发错误:
class foo {
public bar;
constructor() {
this.bar = 'Bar!'; //Expecting 'Bar!' when run foo.bar()
}
//However, it throws an error here, as bar was already defined
bar() {
return 'Function bar() was called';
}
}
[我什至尝试查看setter和getter,因为他们也可以识别是调用了foo.bar
还是foo.bar()
,但是setter不接受返回的值。
我怎么做Firebase团队正在做的事情?他们是如何解决这个魔术的?
可以为功能分配其他属性。
function auth () {
return ‘banana’
}
auth.Auth = ‘wookie’;
console.log(auth()); // banana
console.log(auth.Auth); // wookie
伙计,您已经注意到类的等效于firebase的样式应该像下面,只是您做错了。
class foo {
constructor() {
this.bar.bar = 'Bar!';
// instead of this.bar = 'Bar!';
}
bar() {
return 'Function bar() was called';
}
}
但是。如果必须