oo javascript 具有来自服务器的属性、来自缓存的方法,最佳实践?
我正在将程序 JS 转换为 OO,希望得到任何帮助。 简而言之,我所拥有的是一个 html 页面,其中包含:
<script type="text/javascript">
var serverTime='11:32:20'; //time generated by server (php)
</script>
<script scr="myProcFuncs.js" type="text/javascript">
/* which is containing procedural functions such as
function getServerTime() {return window.serverTime;}
*/
</script>
我喜欢做的是清理,而不增加流量,或多或少......
<script type="text/javascript">
function myOb() {
this.serverTime = '11:32:20';
this.serverDate = '2010-09-24';
}
//first question, need/recommended to create a class??
var myCl = myOb();
</script>
<script scr="myMethods.js" type="text/javascript">
//second question, how to append methods to initiated class or object?
</script>
我要求的不仅是有效的方法,而且是最佳实践OO-JS。还请考虑延迟加载外部 myMethods.js 等...
我正在考虑的选项是:
§1,例如,向启动的类(或静态对象,如果可能)添加方法,如果是这样,请发布附加方法的示例。
§2(最坏情况)使用两个对象,一个用于属性(服务器生成),另一个用于方法。
感谢您对此事的任何启发,祝一切顺利
//汤姆·乔德
I'm converting procedural JS to OO and would appreciate any help.
In a nutshell, what I have is a html-page containing:
<script type="text/javascript">
var serverTime='11:32:20'; //time generated by server (php)
</script>
<script scr="myProcFuncs.js" type="text/javascript">
/* which is containing procedural functions such as
function getServerTime() {return window.serverTime;}
*/
</script>
What I like to do is a clean up, without increasing traffic, more or less...
<script type="text/javascript">
function myOb() {
this.serverTime = '11:32:20';
this.serverDate = '2010-09-24';
}
//first question, need/recommended to create a class??
var myCl = myOb();
</script>
<script scr="myMethods.js" type="text/javascript">
//second question, how to append methods to initiated class or object?
</script>
What I'm asking for is not only what works, but best practice in the OO-JS. Please also concider delayed loading of external myMethods.js and so on...
Options I'm concidering are:
§1, as example, add methods to initiated class (or static object if possible), and if so, please post example of appending method.
§2 (worst case) use two objects, one for properties (server generated), and one for the methods.
Thankful for any light in this matter, all the best
//Tom Joad
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是行不通的。
this
仅在函数中作为对象的方法调用或使用new
运算符调用时才有意义。如果您只是执行
myOb()
而不是new myOb()
,那么this
将是全局(window
) 对象并分配 this.serverTime 会有效地创建全局变量,这是您首先想要避免的。 (同样,如果没有函数的返回值,myCl
将是未定义
。)由于您似乎并没有真正执行任何需要多个实例或原型设计的操作,因此请忘记函数并仅使用对象文字:
您可以使用服务器端的 JSON 编码器轻松生成这样的代码。例如,如果您使用的服务器端语言是 PHP:
使用内联函数表达式:
但是我不确定这是否真的能给你带来任何好处。 JavaScript 是一种混合脚本语言,您不必像 Java 那样思考并尝试将所有内容强制放入对象和类中。 (特别是因为您实际上并没有获得课程,并且必须使用原型来推出自己的课程。)
This doesn't work.
this
only has meaning in a function if it's being called as a method on an object, or with thenew
operator.If you just do
myOb()
instead ofnew myOb()
, thenthis
will be the global (window
) object and assigningthis.serverTime
is effectively creating global variables, what you were trying to avoid in the first place. (Also without a return value from the function,myCl
will beundefined
.)Since you don't really seem to be doing anything that requires multiple instances or prototyping, forget the function and just use an object literal:
you can easily generate code like this using a JSON encoder on the server side. For example if the server-side language you're using were PHP:
Use an inline function expression:
However I'm not sure this really gets you anything. JavaScript is a mixed scripting language, you don't have to think like Java and try to force everything into objects and classes. (Especially since you don't actually get classes, and have to roll your own using prototyping.)
现在解决了,(并修复了一些拼写错误......):
有效。如果有人知道更好的方法,请留言。
Solved for now with, (and fixed some typos...):
Works. If anyone knows a better way, please post.