我正在考虑创建自己的JavaScript客户端库,我喜欢Firebase对请求进行格式化的方式。我在试着了解到底发生了什么。通过查看网络指南这里,我发现了以下代码:
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也许我完全走错了路,我只是不熟悉这个模式。
发布于 2015-10-25 14:47:39
如果您检查Firebase,您将看到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
https://stackoverflow.com/questions/33329393
复制相似问题