oo javascript 具有来自服务器的属性、来自缓存的方法,最佳实践?

发布于 2024-09-25 13:03:46 字数 1081 浏览 3 评论 0原文

我正在将程序 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 技术交流群。

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

发布评论

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

评论(2

欢你一世 2024-10-02 13:03:46
function myOb() {
    this.serverTime = '11:32:20';

这是行不通的。 this 仅在函数中作为对象的方法调用或使用 new 运算符调用时才有意义。

如果您只是执行 myOb() 而不是 new myOb(),那么 this 将是全局(window ) 对象并分配 this.serverTime 会有效地创建全局变量,这是您首先想要避免的。 (同样,如果没有函数的返回值,myCl 将是未定义。)

由于您似乎并没有真正执行任何需要多个实例或原型设计的操作,因此请忘记函数并仅使用对象文字:

var pageValues= {
    serverTime: '11:32:20',
    serverDate: '2010-09-24'
};

您可以使用服务器端的 JSON 编码器轻松生成这样的代码。例如,如果您使用的服务器端语言是 PHP:

<?php
    $pageValues= array('serverTime'=>'11:32:20', 'serverDate'=>'2010-09-24');
?>
<script type="text/javascript">
    var pageValues= <?php echo json_encode($pageValues); ?>;
</script>

如何将方法附加到启动的类或对象?

使用内联函数表达式:

pageValues.someMethod= function() {
    ...do something...
};

但是我不确定这是否真的能给你带来任何好处。 JavaScript 是一种混合脚本语言,您不必像 Java 那样思考并尝试将所有内容强制放入对象和类中。 (特别是因为您实际上并没有获得课程,并且必须使用原型来推出自己的课程。)

function myOb() {
    this.serverTime = '11:32:20';

This doesn't work. this only has meaning in a function if it's being called as a method on an object, or with the new operator.

If you just do myOb() instead of new myOb(), then this will be the global (window) object and assigning this.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 be undefined.)

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:

var pageValues= {
    serverTime: '11:32:20',
    serverDate: '2010-09-24'
};

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:

<?php
    $pageValues= array('serverTime'=>'11:32:20', 'serverDate'=>'2010-09-24');
?>
<script type="text/javascript">
    var pageValues= <?php echo json_encode($pageValues); ?>;
</script>

how to append methods to initiated class or object?

Use an inline function expression:

pageValues.someMethod= function() {
    ...do something...
};

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.)

梦一生花开无言 2024-10-02 13:03:46

现在解决了,(并修复了一些拼写错误......):

<script type="text/javascript">
  function myCl() {
    this.serverTime = '11:32:20';
    this.serverDate = '2010-09-24';
  }
  var myOb = new myCl();
</script>
<script scr="myMethods.js" type="text/javascript">
  /* myMethods.js containing methods as..
  myOb.getTime = function() {
    return this.serverTime;
  } 
  */
</script>

有效。如果有人知道更好的方法,请留言。

Solved for now with, (and fixed some typos...):

<script type="text/javascript">
  function myCl() {
    this.serverTime = '11:32:20';
    this.serverDate = '2010-09-24';
  }
  var myOb = new myCl();
</script>
<script scr="myMethods.js" type="text/javascript">
  /* myMethods.js containing methods as..
  myOb.getTime = function() {
    return this.serverTime;
  } 
  */
</script>

Works. If anyone knows a better way, please post.

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