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

Bil*_*ill 5 javascript static-libraries javascript-objects firebase

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

});
Run Code Online (Sandbox Code Playgroud)

我可以看到它ref等于一个被调用的函数Firebase,并且usersRef等于ref.child.

我想象的是这样的:

 Firebase = function(url) {
    this.child = function(path) {

  console.log(url);
  console.log(path);

};
};
Run Code Online (Sandbox Code Playgroud)

在这里,我可以看到usersRef.set被调用,但我无法弄清楚这将如何或在何处发生?是set函数还是对象?我注意到火力有set(),update(),push()transaction(),让我觉得这些都是功能.

"TypeError: Cannot read property 'set' of undefined
Run Code Online (Sandbox Code Playgroud)

也许我完全走错了路,我只是不熟悉这种模式.

Fra*_*len 1

如果您检查 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);
   };

};
Run Code Online (Sandbox Code Playgroud)

我已经更新了你的 jsbin:https ://jsbin.com/nucume/2/edit?js,console