构建我的 JS 库,几个问题
我正在构建一个库(https://github.com/OscarGodson/storageLocker),更准确地说,是一个 localStorage 包装器,但因为这是我第一次尝试 OO JavaScript,所以我仍在学习,并且有几个问题。
我在其他库中看到有时他们将它们包装在匿名函数中。我应该这样做吗?或者我应该这样做吗?如果是这样,如何在不破坏任何东西的情况下?
对于内部API(基本上是内部函数)我应该如何编写它们?应该将它们添加到主对象,例如
storageLocker.prototype.myInternalFunction()
还是只是在我的脚本中随机添加到myInternalFunction()
?但我不希望这些函数是全局的...例如,其中一个函数只是检查 JSON 中的一堆项目,查看它们的对象,然后检查对象类型是什么(例如Date( )
),然后对其进行转换。我应该如何/在哪里添加全局变量到我的脚本中?例如,我有一个名为
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.
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?
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 justmyInternalFunction()
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 (likeDate()
) and then converts it.How/where should I add global, to my script, vars? e.g. i have a var called
patterns
that is something likevar 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想说:
1)这种技术的目的不是污染全局命名空间。这是一件好事。
在下面的示例中,您可以看到与库的所有交互都是通过一个对象 MyLibrary 进行的。公共API是匿名函数的返回值。
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.
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"
有一个关于命名空间的不错的部分值得一读。
也是另一个很好的概述。
有关良好 javascript 实践的更多精彩材料,请查看
经过我们在评论中的讨论,我将示例更改为this:
虽然这并不完全是库/模块的示例(因为您通过直接调用构造函数来访问代码),但它是一个将同一对象保留在范围内以进一步链接方法的示例。实际上它并没有返回 storageLocker 对象,而是返回“public”对象,但是该对象可以通过闭包访问 storageLocker 的作用域。
可能还有其他更好的方法可以通过返回 storageLocker 对象本身来实现此目的,但这需要更多的思考。
has a decent section on namespacing worth reading.
is also another good overview.
For more great material on good javascript practices, check out
After our discussion in the comments, I've changed the example to this:
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 thestorageLocker
'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.