编写一个代码完成和代码检查友好的Javascript库(Writing a Javascript library that is code

编程入门 行业动态 更新时间:2024-10-20 20:58:49
编写一个代码完成和代码检查友好的Javascript库(Writing a Javascript library that is code-completion and code-inspection friendly)

我最近创建了自己的Javascript库,最初我使用了以下模式:

var myLibrary = (function () { var someProp = "..."; function someFunc() { ... } function someFunc2() { ... } return { func: someFunc, fun2: someFunc2, prop: someProp; } }());

这个问题是我无法真正使用代码完成,因为IDE不知道函数文字返回的属性(我顺便使用IntelliJ IDEA 9)。

我查看了jQuery代码并试图这样做:

(function(window, undefined) { var myLibrary = (function () { var someProp = "..."; function someFunc() { ... } function someFunc2() { ... } return { func: someFunc, fun2: someFunc2, prop: someProp; } }()); window.myLibrary = myLibrary; }(window));

我尝试过这个,但现在我有一个不同的问题。 IDE也没有真正接受myLibrary 。

我现在解决问题的方式是这样的:

var myLibrary = { func: function() { }, func2: function() { }, prop: "" }; myLibrary = (function () { var someProp = "..."; function someFunc() { ... } function someFunc2() { ... } return { func: someFunc, fun2: someFunc2, prop: someProp; } }());

但这似乎有点笨重,我无法弄清楚jQuery是如何做到的。 我的另一个问题是如何使用任意数量的参数处理函数。

例如, jQuery.bind可以使用2或3个参数,IDE似乎没有抱怨。 我试图用我的库做同样的事情,其中​​一个函数可以带0个参数或1个参数。 但是,IDE会抱怨并警告没有发送正确数量的参数。如何处理?

编辑

我开始怀疑这是否是一个Idea9问题,因为jQuery有同样的问题。 我似乎在其他项目中没有这个问题。

I recently made my own Javascript library and I initially used the following pattern:

var myLibrary = (function () { var someProp = "..."; function someFunc() { ... } function someFunc2() { ... } return { func: someFunc, fun2: someFunc2, prop: someProp; } }());

The problem with this is that I can't really use code completion because the IDE doesn't know about the properties that the function literal is returning (I'm using IntelliJ IDEA 9 by the way).

I've looked at jQuery code and tried to do this:

(function(window, undefined) { var myLibrary = (function () { var someProp = "..."; function someFunc() { ... } function someFunc2() { ... } return { func: someFunc, fun2: someFunc2, prop: someProp; } }()); window.myLibrary = myLibrary; }(window));

I tried this, but now I have a different problem. The IDE doesn't really pick up on myLibrary either.

The way I'm solving the problem now is this way:

var myLibrary = { func: function() { }, func2: function() { }, prop: "" }; myLibrary = (function () { var someProp = "..."; function someFunc() { ... } function someFunc2() { ... } return { func: someFunc, fun2: someFunc2, prop: someProp; } }());

But that seems kinda clunky, and I can't exactly figure out how jQuery is doing it. Another question I have is how to handle functions with arbitrary numbers of parameters.

For example, jQuery.bind can take 2 or 3 parameters, and the IDE doesn't seem to complain. I tried to do the same thing with my library, where a function could take 0 arguments or 1 argument. However, the IDE complains and warns that the correct number of parameters aren't being sent in. How do I handle this?

EDIT

I'm starting to wonder if this is an Idea9 issue because jQuery has the same problem. I don't seem to have this problem in other projects though.

最满意答案

我正在使用IDEA与雅虎模块模式和我的自动完成功能。 谷歌为雅虎模块模式。

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

http://ajaxian.com/archives/a-javascript-module-pattern


TEST = function() { var SOME_CONSTANT='asd'; function privateStuff(){ var a = 'asd'; return a; } return{ someArray:[], someMethod: function(foo, bar){ var foo = *1 } , myProperty:'test' } }(); TEST.*2

与* 1和* 2我标记我尝试自动完成的地方。

在* 1我得到SOME_CONSTANT和privateStuff方法,如果我把它。(自动完成)我可以访问return {}块内的所有方法和属性

当我在* 2上尝试自动完成时,我获得了返回{}块内的所有方法和属性。 SOME_CONSTANT和privateStuff方法在那里是不可见的,因为它们是“私有的”。

对我来说,自动完成程度非常好。

I'm using IDEA with yahoo module pattern and my autocomplete works. Google for yahoo module pattern.

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

http://ajaxian.com/archives/a-javascript-module-pattern


TEST = function() { var SOME_CONSTANT='asd'; function privateStuff(){ var a = 'asd'; return a; } return{ someArray:[], someMethod: function(foo, bar){ var foo = *1 } , myProperty:'test' } }(); TEST.*2

with *1 and *2 i marked places where i tried auto complete.

in *1 i get SOME_CONSTANT and privateStuff method, and if i put this.(autocomplete) i get access to all the methods and properties inside of return {} block

when i try autocomplete on *2 i get all the methods and properties inside return {} block. SOME_CONSTANT and privateStuff method are invisibile there, because they are "private".

For me that level of autocomplete is quite fine.

更多推荐

本文发布于:2023-08-01 03:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1353761.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   友好   Javascript   code   library

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!