模拟Firebase客户端库的结构和模式

Mimic Firebase client library's structure and patterns

本文关键字:结构 模式 Firebase 客户端 模拟      更新时间:2023-09-26

我正在考虑制作我自己的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