构建我的 JS 库,几个问题

发布于 2024-10-05 02:02:44 字数 740 浏览 2 评论 0原文

我正在构建一个库(https://github.com/OscarGodson/storageLocker),更准确地说,是一个 localStorage 包装器,但因为这是我第一次尝试 OO JavaScript,所以我仍在学习,并且有几个问题。

  1. 我在其他库中看到有时他们将它们包装在匿名函数中。我应该这样做吗?或者我应该这样做吗?如果是这样,如何在不破坏任何东西的情况下?

  2. 对于内部API(基本上是内部函数)我应该如何编写它们?应该将它们添加到主对象,例如 storageLocker.prototype.myInternalFunction() 还是只是在我的脚本中随机添加到 myInternalFunction() ?但我不希望这些函数是全局的...例如,其中一个函数只是检查 JSON 中的一堆项目,查看它们的对象,然后检查对象类型是什么(例如 Date( )),然后对其进行转换。

  3. 我应该如何/在哪里添加全局变量到我的脚本中?例如,我有一个名为 patterns 的 var,类似于 varpatterns = {"date":/\/Date\(([0-9]+)\)\//}< /code> 我应该如何将其添加到我的脚本中?

多谢。我想以正确的方式编写我的脚本,所以我问你们。我不知道本地有哪个 JS 人会做 OO JS,他们都是老派类型......

I'm building a library (https://github.com/OscarGodson/storageLocker), a localStorage wrapper to be more exact, but because this is my first try at OO JavaScript I'm still learning and I have a couple questions.

  1. I've seen in other libraries that sometimes they wrap them in a anonymous function. Do I, or should I, do that with this? And if so, how without breaking anything?

  2. For the internal API (basically, the internal functions) how should I write them? Should add them to the main object e.g. storageLocker.prototype.myInternalFunction() or just myInternalFunction() randomly in my script? I didn't want the functions to be global though... One of the functions for example just checks a bunch of items in the JSON, sees if their objects, and then checks what the object type is (like Date()) and then converts it.

  3. How/where should I add global, to my script, vars? e.g. i have a var called patterns that is something like var patterns = {"date":/\/Date\(([0-9]+)\)\//} how should I add that into my script?

Thanks a lot. I want to write my script the right way so im asking you guys. I don't know of any JS guys locally that do any OO JS, they're all old school types...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅听莫相离 2024-10-12 02:02:44

我想说:

1)这种技术的目的不是污染全局命名空间。这是一件好事。
在下面的示例中,您可以看到与库的所有交互都是通过一个对象 MyLibrary 进行的。公共API是匿名函数的返回值。

var MyLibrary = function() {
// private
   this.InternalVariable = 'some value';

   function internalFunction(x,y) {
      return x + y;
      }

     function getInternalVariable() {
    return this.InternalVariable;
}
// public
   return {
      publicVariable : '1.0',
      publicFunction : function(x,y) {
         return x + y
         },
 accessInternalVariable : function() {

     return getInternalVariable();
     }
      }
   }();

2)另请参阅上面关于如何放置“内部”函数的示例

3)如果全局变量是某种配置选项,我只需将其设为公共 setter/getter 并将变量保留为“私有”

I'd say:

1) the purpose of this technique is not pollute the global namespace. That is a good thing.
In the example below you can see that all your interaction with the library is via one object MyLibrary. Public API is the return value of the anonymous function.

var MyLibrary = function() {
// private
   this.InternalVariable = 'some value';

   function internalFunction(x,y) {
      return x + y;
      }

     function getInternalVariable() {
    return this.InternalVariable;
}
// public
   return {
      publicVariable : '1.0',
      publicFunction : function(x,y) {
         return x + y
         },
 accessInternalVariable : function() {

     return getInternalVariable();
     }
      }
   }();

2) see also the example above on how to place your "internal" functions

3) if you global variable is some kind of a configuration option, I'd just make public setter/getter and kept the variable "private"

回忆凄美了谁 2024-10-12 02:02:44

http:// /nefariousdesigns.co.uk/archive/2010/10/object-orient-javascript-follow-up-part-2-technical/

有一个关于命名空间的不错的部分值得一读。

http://yuiblog.com/blog/2007/06/12 /模块模式/

也是另一个很好的概述。

有关良好 javascript 实践的更多精彩材料,请查看

http://javascript.crockford.com/

经过我们在评论中的讨论,我将示例更改为this:

var storageLocker = function (selector) {

    var _selector = selector || "default value";

    function myPrivateFunction() {

    }

    var public = {

        get: function () {
            return _selector;
        },

        uppercase : function () {
            _selector = _selector.toUpperCase()
            return this;
        }

    }
    return public;

};

// use:
var test = storageLocker("search for this").uppercase().get();;

alert(test);

虽然这并不完全是库/模块的示例(因为您通过直接调用构造函数来访问代码),但它是一个将同一对象保留在范围内以进一步链接方法的示例。实际上它并没有返回 storageLocker 对象,而是返回“public”对象,但是该对象可以通过闭包访问 storageLocker 的作用域。

可能还有其他更好的方法可以通过返回 storageLocker 对象本身来实现此目的,但这需要更多的思考。

http://nefariousdesigns.co.uk/archive/2010/10/object-oriented-javascript-follow-up-part-2-technical/

has a decent section on namespacing worth reading.

http://yuiblog.com/blog/2007/06/12/module-pattern/

is also another good overview.

For more great material on good javascript practices, check out

http://javascript.crockford.com/

After our discussion in the comments, I've changed the example to this:

var storageLocker = function (selector) {

    var _selector = selector || "default value";

    function myPrivateFunction() {

    }

    var public = {

        get: function () {
            return _selector;
        },

        uppercase : function () {
            _selector = _selector.toUpperCase()
            return this;
        }

    }
    return public;

};

// use:
var test = storageLocker("search for this").uppercase().get();;

alert(test);

While this isn't exactly an example of a library/module (because you're accessing the code by calling the constructor directly), it is an example of keeping the same object in scope for further chaining of methods. Actually it's not returning the storageLocker object, it's returning the 'public' object, but that object has access to the storageLocker's scope through closure.

There could be other better ways to do this by perhaps returning the storageLocker object itself, but that would require a bit more thinking through.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文