请求代码布局的评论 [英] Comments on code layout requested

查看:71
本文介绍了请求代码布局的评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。


你能给我一些关于我的代码布局的反馈吗?


我把所有东西都组织在一个对象中, NRL,用作命名空间,

,下面还有其他子对象/模块,例如对话框

或ajax。我对一般性评论感兴趣,并且我有一些特定的

问题,但首先是(示例)代码:


var NRL = {};

......


NRL.ajax = {};

......


NRL.ajax.ListControl = function(arg1,arg2){


//首先,一些本地常量" (配置)

var PAGER_BUFFER = 5,

EXPERIMENTAL_SORTING = false,

.....;


//接下来,公众成员

this.paused = false;

this.currFoo ="" ;;

.....


//下一步公共方法

this.setTrigger = function(src){

... ..

};

this.fetchRows = function(argX,argY){

.....

};


/////" private"在这行之后/////////////////////


var节点,标题,.....,
.....

spinner = NRL.ajax.Spinner.getInstance(),

= =;


//只能在此范围内/下方访问的私人函数

函数analyzeHeader(){

.....

}

函数receiveUpdate(jsonData){

.....

}

......


/////一些初始化(不是在所有对象中)//////////


node = document .createElement(" div");

.....


};

它运作良好,目前为止正如我所看到的,我喜欢将公共

和私人分开。源头区域。当其他人阅读代码时,他们可以立即看到公共接口的位置。结束。一些问题:


1)我有点担心许多关闭......这会占用大量的内存吗?如果是这样,我怎样才能改善布局?一些函数

也有其他内部函数,比如当我为XMLHttpRequest添加事件处理程序或

回调时,它们也可以作为闭包。


2)我见过人们使用函数表达式而不是函数

语句为私有功能。在这种情况下,函数表达式是否具有任何

优势?


3)大多数对象都不需要它,但最好的是什么通过

让其他对象从这样的结构继承?


4)这些对象中的一些是单例,比如NRL.ajax.Spinner

上面的例子。我目前这样处理它们:


NRL.ajax.Spinner = function(){

.....与上面相同.....

};

NRL.ajax.Spinner.getInstance = function(){

var instance;

return function(){

instance = instance ||新的NRL.ajax.Spinner();

返回实例;

}();

};


现在有人仍然可以使用new NRL.ajax.Spinner()直接,但是通过向Spinner构造函数添加一个检查可以避免
。我不希望

为单例创建一个对象文字,除非确实需要一个对象(实例)

,而且构造函数可以初始化

代码,比如创建元素。

谢谢大家,

Dano

解决方案

< blockquote> Daniel Norden写道:


你能给我一些关于我的代码布局的反馈吗?



好​​吧......这并不像我希望的那样好。我可能一次问了太多

的问题。我打算再试一次,这次只问一个简短的

问题。我真的很感激JS专家的一些反馈

在这个组....如果你需要更多的细节,我原来的帖子仍然是

那里[ 0];回答任何一个都会很棒。


你看到像这样的构造函数有什么常见问题吗?


var ListControl = function( arg1,arg2){

//公众成员

this.paused = false;

.....


//公共方法

this.setTrigger = function(src){.....};

.....

/////私人在这行之后/////////////////////

var node,header,that = this;


//私有函数

函数analyzeHeader(){.....}

......


/////一些初始化(不是在所有构造函数中)//////////

node = document.createElement(" div");

.....

};


var ctrl = new ListControl(x,y);

谢谢提前,

Dano

[0] msg id< gd ********** @ aioe.org>
http://groups.google.com/group/comp .. ..9ab340d8cd1d85


Daniel Norden写道:


Daniel Norden写道:


>你能给我一些关于我的代码布局的反馈吗?



好​​的......这并不像我希望的那样好。我可能一次问了太多

的问题。 [...]



或许你没有等待足够长的时间:)周末是*现在*。你可以期待

a更详细的回复,至少从我这里回来。

PointedEars

-

使用用于创建网站的任何版本的Microsoft Frontpage。

(这不会阻止人们查看您的来源,但没有人

会想要窃取它。)

- 来自< http://www.vortex-webdesign.com/help/hidesource.htm>


Daniel Norden写道:


我将所有内容组织在一个对象中,NRL,用作命名空间,

以及其他子对象/下面的模块,例如对话框

或ajax。我对一般性评论感兴趣,并且我有一些特定的

问题,但首先是(示例)代码:


var NRL = {};



由于标识符指的是不能[[Construct]]编辑的对象,因此它不应该以大写字母开头。


.....


NRL.ajax = {};

.. ......


NRL.ajax.ListControl = function(arg1,arg2){



你可以写得更简洁更容易维护,没有缺点

本身:


var NRL = {

ajax:{

ListControl:function(...){

...

}

}

} ;


请注意,您可以创建名称空间也有前缀。


[...]

//接下来,公众成员



通常应该通过原型对象将它们移动并初始化。

因此,在构造新对象时,只需要将属性初始化为
哪个值应该与继承的值不同,例如

,前者取决于构造函数参数。


NRL.ajax.Spinner.prototype = {

暂停:false

};


特别是使用方法,这可以提高内存效率,因为你只有
所有实例使用的一个函数对象。但是,它也会稍微降低运行时效率,因为所有调用沿着

原型链查找需要更多步骤。由于内存比现在的编程更加昂贵,我建议使用

原型链模式。


[...]

/////" private"在这行之后/////////////////////


var节点,标题,.....,
.....

spinner = NRL.ajax.Spinner.getInstance(),



有什么意义单例模式如果你不能阻止创建

从同一个对象继承的新对象?


that = this;


//只能在此范围内/下方访问的私人函数

函数analyzeHeader(){

.....

}

函数receiveUpdate(jsonData){

.....

}



如果你在公共方法中使用

,那些函数只会在经典意义上变为私有。


.. ....


/////一些初始化(不是在所有对象中)//////////


node = document.createElement(" div");

.....



如果变量的声明和

初始化不在同一个语句中,我发现维护源代码更难。

(当然也有一些例外。)


[...]

1)我有点担心很多关闭......那用完了吗?b $ ba大量的内存?



这取决于你所考虑的巨大数字应用程序运行时创建了多少个对象



如果是这样,我该如何改进布局?



您可以尝试在FAQ注释中应用关闭的建议。


一些函数

还有其他内部函数,比如当我添加事件处理程序或

回调XMLHttpRequest时,它们也充当闭包。



这些不被视为内部函数,但你必须注意那里涉及DOM对象的

闭包,特别是如果这应该在基于MSHTML的UA中运行。


2)我见过人们使用函数表达式而不是函数

私人的陈述功能。在这种情况下,函数表达式是否具有任何

优势?



如果条件执行的可能性(没有肮脏的黑客)和使用

初始化值算作优势,那么是。我们之前讨论了这个



3)大部分物品都不需要它,但最好的是什么

的方式让其他对象从这样的结构继承?



Google这个新闻组用于继承。我们已经讨论过这个广告了。



4)其中一些对象是单例,如NRL.ajax.Spinner in上面的

例子。我目前这样处理它们:


NRL.ajax.Spinner = function(){

.....与上面相同.....

};

NRL.ajax.Spinner.getInstance = function(){

var instance;

return function(){

instance = instance ||新的NRL.ajax.Spinner();

返回实例;

}();

};


现在有人仍然可以使用new NRL.ajax.Spinner()直接,但是通过向Spinner构造函数添加一个检查可以避免
。除非一个对象(实例)确实需要
,否则我不希望

为单例创建一个对象文字,



当然你需要另一个对象来计算创建的实例数。

理想情况下,该对象将是构造函数的原型对象或

构造函数本身。


除了构造函数可以有初始化代码,比如创建

元素。



如果你想防止有多个相同的对象,单例模式是有用的(如果在这些动态的
语言中毫无意义) br />
继承。你为什么要拒​​绝这个库的用户

创建几个微调器?

PointedEars

-

任何打击此页面的人最好用浏览器X查看标签

a网页似乎渴望过去的糟糕时光,在网络之前,

当你几乎没有机会阅读另一台计算机,另一台文字处理器或其他网络上的文件。 - Tim Berners-Lee


Hello.

Can you give me some feedback on my code layout?

I have everything organized in an object, "NRL", that''s used as a namespace,
and there are other subobjects/modules below that, for example "dialog"
or "ajax". I''m interested in general comments, and I have some specific
questions, but first the (sample) code:

var NRL = {};
......

NRL.ajax = {};
......

NRL.ajax.ListControl = function (arg1, arg2) {

// first, some local "constants" (configuration)
var PAGER_BUFFER = 5,
EXPERIMENTAL_SORTING = false,
.....;

// next, the public members
this.paused = false;
this.currFoo = "";
.....

// public methods next
this.setTrigger = function (src) {
.....
};
this.fetchRows = function (argX, argY) {
.....
};

///// "private" after this line /////////////////////

var node, header, .....,
.....
spinner = NRL.ajax.Spinner.getInstance(),
that = this;

// private functions only accessible in/below this scope
function analyzeHeader () {
.....
}
function receiveUpdate (jsonData) {
.....
}
......

///// some initializations (not in all objects) //////////

node = document.createElement("div");
.....

};
It works well, as far as I can see, and I like the separation of "public"
and "private" areas in the source. When other people read the code, they
can immediately see where the public "interface" ends. Some questions:

1) I''m a little worried about the many closures... does that use up a huge
amount of memory? If so how can I improve the layout? Some of the functions
have other inner functions as well, like when I add event handlers or
callbacks for XMLHttpRequest, that also act as closures.

2) I have seen people using function expressions instead of function
statements for the "private" functions. Do function expressions have any
advantage in this case?

3) Most of the objects don''t ever need it, but what would be the best way to
let other objects inherit from a structure like this?

4) Some of these objects are singletons, like the NRL.ajax.Spinner in the
example above. I currently handle them like this:

NRL.ajax.Spinner = function () {
..... same as above .....
};
NRL.ajax.Spinner.getInstance = function () {
var instance;
return function () {
instance = instance || new NRL.ajax.Spinner();
return instance;
}();
};

Now somebody could still use "new NRL.ajax.Spinner()" directly, but that
could be avoided by adding a check to the Spinner constructor. I don''t want
to create an object literal for the singleton unless an object (instance)
is really required, and besides the constructor could have initialization
code, like creating elements.
Thanks everybody,
Dano

解决方案

Daniel Norden wrote:

Can you give me some feedback on my code layout?

Okay... that didn''t go as well as I''d hoped. I probably asked too many
questions at once. I''m going to give it another shot and just ask one short
question this time. I''d really appreciate some feedback from the JS pundits
in this group.... If you need more detail, my original post''s still
there[0]; a reply to either one would be great.

Do you see any general problems with a constructor like this?

var ListControl = function (arg1, arg2) {
// public members
this.paused = false;
.....

// public methods
this.setTrigger = function (src) { ..... };
.....

///// "private" after this line /////////////////////
var node, header, that = this;

// private functions
function analyzeHeader () { ..... }
......

///// some initializations (not in all constructors) //////////
node = document.createElement("div");
.....
};

var ctrl = new ListControl(x,y);
Thanks in advance,
Dano
[0] msg id <gd**********@aioe.org>
http://groups.google.com/group/comp....9ab340d8cd1d85


Daniel Norden wrote:

Daniel Norden wrote:

>Can you give me some feedback on my code layout?


Okay... that didn''t go as well as I''d hoped. I probably asked too many
questions at once. [...]

Or maybe you did not wait long enough :) Weekend is *now*. You can expect
a more verbose reply, at least from me, later.
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won''t prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>


Daniel Norden wrote:

I have everything organized in an object, "NRL", that''s used as a namespace,
and there are other subobjects/modules below that, for example "dialog"
or "ajax". I''m interested in general comments, and I have some specific
questions, but first the (sample) code:

var NRL = {};

Since the identifier refers to an object that cannot be [[Construct]]ed, it
should not start with a capital letter.

.....

NRL.ajax = {};
.....

NRL.ajax.ListControl = function (arg1, arg2) {

You can write this more concise and easier to maintain, without drawbacks in
itself:

var NRL = {
ajax: {
ListControl: function(...) {
...
}
}
};

Note that you can create "namespaces" with prefixes as well.

[...]
// next, the public members

Those should usually be moved to and initialized through a prototype object.
Thus, on construction of the new object only properties need to be
initialized which value should differ from the inherited value, e.g.
where the former depends on the constructor arguments.

NRL.ajax.Spinner.prototype = {
paused: false
};

Especially with methods this improves memory efficiency since you have only
one function object that is used by all instances. However, it would also
slightly decrease runtime efficiency as for all calls the lookup along the
prototype chain requires more steps. Since memory is more expensive than
CPU speed with regard to programming nowadays, I recommend to use the
prototype chain pattern where applicable.

[...]
///// "private" after this line /////////////////////

var node, header, .....,
.....
spinner = NRL.ajax.Spinner.getInstance(),

What sense does a singleton pattern make if you cannot prevent the creation
of new objects that inherit from the same object?

that = this;

// private functions only accessible in/below this scope
function analyzeHeader () {
.....
}
function receiveUpdate (jsonData) {
.....
}

Those functions only become private in the classical sense if you use them
in public methods.

......

///// some initializations (not in all objects) //////////

node = document.createElement("div");
.....

I find it harder to maintain source code if the declaration and
initialization of a variable are not in the same statement.
(There are a few exceptions, of course.)

[...]
1) I''m a little worried about the many closures... does that use up
a huge amount of memory?

That depends on what you consider "huge" and how many objects are created
while the application is running.

If so how can I improve the layout?

You can try applying the suggestions on closures in the FAQ Notes.

Some of the functions
have other inner functions as well, like when I add event handlers or
callbacks for XMLHttpRequest, that also act as closures.

Those are not considered "inner functions", but you have to look out for
closures that involve DOM objects there as well, especially if this should
run in a MSHTML-based UA.

2) I have seen people using function expressions instead of function
statements for the "private" functions. Do function expressions have any
advantage in this case?

If the possibility of conditional execution (without dirty hacks) and to use
initialized values counts as an advantage, then yes. We have discussed this
before.

3) Most of the objects don''t ever need it, but what would be the best way to
let other objects inherit from a structure like this?

Google this newsgroup for "inheritance". We have discussed this ad nauseam
as well.

4) Some of these objects are singletons, like the NRL.ajax.Spinner in the
example above. I currently handle them like this:

NRL.ajax.Spinner = function () {
..... same as above .....
};
NRL.ajax.Spinner.getInstance = function () {
var instance;
return function () {
instance = instance || new NRL.ajax.Spinner();
return instance;
}();
};

Now somebody could still use "new NRL.ajax.Spinner()" directly, but that
could be avoided by adding a check to the Spinner constructor. I don''t want
to create an object literal for the singleton unless an object (instance)
is really required,

Of course you need another object to count the number of instances created.
Ideally that object would be the constructor''s prototype object or the
constructor itself.

and besides the constructor could have initialization code, like creating
elements.

A singleton pattern is useful (if rather pointless in these dynamic
languages) iff you want to prevent having several objects of the same
inheritance. Why would you want to deny the user of this library
to create several spinners?
PointedEars
--
Anyone who slaps a ''this page is best viewed with Browser X'' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee


这篇关于请求代码布局的评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆