oop - oo javascript with properties from server, methods from cache, best practice? -


I am converting procedural JS into oo and appreciate any help. In short, I have an html page:

  & lt; Script type = "text / javascript" & gt; Var ServerTime = '11: 32: 20 '; // Server generated time (php) & lt; / Script & gt; & Lt; Script scr = "myProcFuncs.js" type = "text / javascript" & gt; / * In which functions are processed as getServerTime () {return window.serverTime;} * / & lt; / Script & gt;  

What I like to do is clear without increasing the traffic, more or less ...

  & lt; Script type = "text / javascript" & gt; Function myOb () {this.serverTime = '11: 32: 20 '; This.serverDate = '2010-09-24'; } // First Question, Need to Create a Class / Recommendation ?? Var myCl = myOb (); & Lt; / Script & gt; & Lt; Script scr = "myMethods.js" type = "text / javascript" & gt; // How to add the second question, the initial class or the methods of the object? & Lt; / Script & gt;  

What I am asking is not only the work, but the best practice in the OO-JS. Please delay the loading of external myMethods.js to the congressor and also ...

The options I am thinking:

  • §1, for example, Addition methods of initial elementary classes (or if possible, static object), and if so, please post an example of the method of adding.

  • §2 (worst position) Use two objects, one for properties (server generated), and one for methods.

Thank you for any light in this case, all the best

// Tom attachment

< Div class = "post-text" itemprop = "text">

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

This does not work this means only in one function if an object is being called as a method, or new < / Code> with operator

If you select new myOb () instead of myOb () then this will be global ( window ) Object and this is making the serverTime global level effectively, which you were trying to avoid from the first place. (Without the return value from the function, myCl will be undefined .)

Since you are not actually doing anything like many examples or prototypes, Forget the function and use only one object literally:

  var page value = {serverTime: '11: 32: 20 ', ServerDate:' 2010-09-24 '};  

You can easily generate code like this on the server side using the JSON encoder. For example, if you are using server-side language then PHP:

  & lt ;? Php $ pageValues ​​= array ('serverTime' => '11: 32: 20 ',' ServerDate = '> 2010-09-24); ? & Gt; & Lt; Script type = "text / javascript" & gt; Var page value = & lt ;? Php echo json_encode ($ pageValues); ? & Gt ;; & Lt; / Script & gt;  

How to add the initial class or object's methods?

Use an inline function expression:

  page value form method = function () {... do something ...};  

Although I'm not sure whether this really is anything to you. Javascript is a mixed scripting language, you do not have to think like Java and try to force all things into objects and classes. (Especially since you do not actually get classes, and you have to roll yourself using the prototype.)


Comments