Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>function myOb() { this.serverTime = '11:32:20'; </code></pre> <p>This doesn't work. <code>this</code> only has meaning in a function if it's being called as a method on an object, or with the <code>new</code> operator.</p> <p>If you just do <code>myOb()</code> instead of <code>new myOb()</code>, then <code>this</code> will be the global (<code>window</code>) object and assigning <code>this.serverTime</code> is effectively creating global variables, what you were trying to avoid in the first place. (Also without a return value from the function, <code>myCl</code> will be <code>undefined</code>.)</p> <p>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:</p> <pre><code>var pageValues= { serverTime: '11:32:20', serverDate: '2010-09-24' }; </code></pre> <p>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:</p> <pre><code>&lt;?php $pageValues= array('serverTime'=&gt;'11:32:20', 'serverDate'=&gt;'2010-09-24'); ?&gt; &lt;script type="text/javascript"&gt; var pageValues= &lt;?php echo json_encode($pageValues); ?&gt;; &lt;/script&gt; </code></pre> <blockquote> <p>how to append methods to initiated class or object?</p> </blockquote> <p>Use an inline function expression:</p> <pre><code>pageValues.someMethod= function() { ...do something... }; </code></pre> <p>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.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload