我正在考虑制作我自己的JavaScript客户端库,我喜欢Firebase格式化请求的方式。我想知道发生了什么。通过查看web指南,我找到了以下代码:
var ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog");
var usersRef = ref.child("users");
usersRef.set({
alanisawesome: {
date_of_birth: "June 23, 1912",
full_name: "Alan Turing"
},
gracehop: {
date_of_birth: "December 9, 1906",
full_name: "Grace Hopper"
}
});
我可以看到,ref
等于一个叫做Firebase
的函数,usersRef
等于ref.child
。
我想象的是这样的:
Firebase = function(url) {
this.child = function(path) {
console.log(url);
console.log(path);
};
};
在这里,我可以看到usersRef.set
正在被调用,但我不能弄清楚这将如何或在哪里?set
是一个函数还是一个对象?我注意到firebase有set()
, update()
, push()
和transaction()
,让我认为这些是函数。
"TypeError: Cannot read property 'set' of undefined
也许我完全走错路了,我只是不熟悉这个模式。
如果检查Firebase API,您将看到child()
返回对子位置的新Firebase引用。像这样:
var Firebase = function(url) {
console.log(url);
this.child = function(path) {
return new Firebase(url+'/'+path);
};
this.set = function(object) {
console.log(object);
};
};
我已经更新了你jsbin: https://jsbin.com/nucume/2/edit?js,console