Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem you are experiencing is the result of confusion about Objects/<em>Classes</em>/Instances in javascript.</p> <p>The critical point for you on this issue is the difference between <code>new game()</code> and <code>game()</code>;</p> <pre><code>var foo = new game() </code></pre> <ul> <li>tells the JS engine to create a new object</li> <li>point that object's Prototype (not <code>prototype</code>) at <code>game</code>'s <code>prototype</code></li> <li>and then invoke the function game, but for the sake of the body of that function <code>this</code> will refer to the created object.</li> <li>If the function doesn't return an object, assign our created object to <code>foo</code> (otherwise assign the function's return value to <code>foo</code></li> </ul> <p>Inside the body of your game function, you have <code>this.cardSelection = function (...</code>. If you simply invoke <code>game</code> as a function, so just <code>game()</code>, without the <code>new</code> keyword, <code>this</code> inside the body of the function will be the window object! So you'll add <code>cardSelection</code> to the window object.</p> <p>Also importantly: <code>game.cardSelection()</code> is looking for a function named <code>cardSelection</code> as a property on the <strong>function</strong> <code>game</code>.</p> <p>Here's an example of using that style that would work:</p> <pre><code>var foo = function () { //do interesting stuff } foo.bar = function () { //do interesting stuff related to foo } foo.bar(); </code></pre> <p>What you seem to be expecting would need to be written this way:</p> <pre><code>var game = function () { this.cardSelection = function () { //perform card selection! } } var aGame = new game(); aGame.cardSelection(); </code></pre> <p>Or, if <code>cardSelection</code> does not need access to any private properties of the game, it could be written more efficiently as</p> <pre><code>var game = function () { //setup the game }; game.prototype.cardSelection = function () { //perform card selection }; var aGame = new game(); aGame.cardSelection(); </code></pre>
    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. 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