Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As @WaleedKhan points out, you shouldn't build a transcompiler for a language you don't know well.</p> <p>First off, your compiled code does not do what you think it does:</p> <pre><code>&gt; var test = { . // Objects != namespaces . // Anything that is not a key: value pair is a syntax error. . function Test() {} . } SyntaxError: Unexpected identifier </code></pre> <p>If you want a namespace-like effect you should use functions, since <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Statements#Block_Statement" rel="nofollow noreferrer"><strong>JavaScript does not have block scope</strong></a>:</p> <pre><code>var test = (function(){ // This wrapper is an immediately invoked function expression (IIFE) // It will act as a "namespace" inasmuch as functions and variables that are // declared with `var` will not leak into the global scope. // However, anything that you don't *explicitly* expose will be private. function Foo() { this.bar = 0.0; } // JavaScript is case-sensative - foo != Foo. Foo.prototype.test = function() { var x = 1; }; return { Foo: Foo }; })(); </code></pre> <p>As to your questions:</p> <p>Yes, <code>Foo</code> is a constructor (of a sort) - however, JavaScript does not support the kind of dispatch you want to do. Instead, you'd need a wrapper function that calls the <em>appropriate</em> constructor depending on the arguments passed in:</p> <pre><code>function FooFactory() { if (typeof arguments[0] === "string") { return new FooString(arguments[0]); } else { return new FooObject(arguments[0]); } } </code></pre> <p>That rapidly gets difficult to work with (though there are <a href="https://github.com/natefaubion/matches.js" rel="nofollow noreferrer">some libraries</a> that make it easier to do such dispatching).</p> <p>As for how to properly do prototype inheritence, have you seen @bobince's answer to <a href="https://stackoverflow.com/questions/1595611/how-to-properly-create-a-custom-object-in-javascript/1598077#1598077">How to &quot;properly&quot; create a custom object in JavaScript?</a></p> <p>Finally, you may want to take a look at <a href="https://stackoverflow.com/questions/111102/how-do-javascript-closures-work">How do JavaScript closures work?</a> as well as Kangax's <a href="http://kangax.github.com/nfe/" rel="nofollow noreferrer">excellent</a> <a href="http://perfectionkills.com/understanding-delete/" rel="nofollow noreferrer">articles</a> on <a href="http://perfectionkills.com/javascript-quiz/" rel="nofollow noreferrer">trickier parts</a> of the JavaScript language (his <a href="http://kangax.github.com/es5-compat-table/" rel="nofollow noreferrer">ES5 compatibility table</a> is not to be missed either).</p> <p>If you made it this far, good luck on your adventure!</p>
    singulars
    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.
    2. VO
      singulars
      1. This table or related slice is empty.
    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