简体   繁体   English

带有服务器属性,缓存方法,最佳实践的oo JavaScript?

[英]oo javascript with properties from server, methods from cache, best practice?

I'm converting procedural JS to OO and would appreciate any help. 我正在将程序JS转换为OO,不胜感激。 In a nutshell, what I have is a html-page containing: 简而言之,我所拥有的是一个包含以下内容的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>

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. 我要问的不仅是什么有效,而且是OO-JS中的最佳实践。 Please also concider delayed loading of external myMethods.js and so on... 还请考虑推迟外部myMethods.js的加载等等。

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. 例如,§1将方法添加到已启动的类(或静态对象,如果可能)中,如果可以,请发布附加方法的示例。

  • §2 (worst case) use two objects, one for properties (server generated), and one for the methods. §2(最坏的情况)使用两个对象,一个用于属性(服务器生成),另一个用于方法。

Thankful for any light in this matter, all the best 谢谢你对此事的任何祝福

//Tom Joad //汤姆·乔德

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. this只是在功能意义,如果它被称为对象的方法,或者与new运营商。

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. 如果你只是做myOb()而不是new myOb()那么this将是全球( window )对象和分配this.serverTime有效地创建全局变量,你试图避免在首位。 (Also without a return value from the function, myCl will be undefined .) (同样,如果没有该函数的返回值, myCl也将是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. 您可以在服务器端使用JSON编码器轻松生成类似这样的代码。 For example if the server-side language you're using were PHP: 例如,如果您使用的服务器端语言是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. JavaScript是一种混合脚本语言,您不必像Java那样思考,而是尝试将所有内容强加到对象和类中。 (Especially since you don't actually get classes, and have to roll your own using prototyping.) (特别是因为您实际上并未获得课程,而不得不使用原型制作自己的课程。)

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. 如果有人知道更好的方法,请发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM