javascript - TypeError: Cannot find function testFunction in object -


I wrote a test function that sits inside my object, but can not access the function. What am I doing wrong? I mean I'm sure the code I wrote is terrible, but especially what is causing the error?

  function player () {this.green = 0; Function testFunction () {this.green = 99; }; };   

The player is created in the object object:

  function game (numplayers) {this.playerArray = []; Switches (numplayers) {Case 2: this.player1 = new player (); This.player2 = new player (); This.playerArray.push (this.player1, this.player2); break; Case 3: It's Player 1 = new player (); This.player2 = new player (); This.player3 = new player (); This.playerArray.push (this.player1, this.player2, this.player3); break; Case 4: It Player 1 = new player (); This.player2 = new player (); This.player3 = new player (); This.player4 = new player (); This.playerArray.push (this.player1, this.player2, this.player3, this.player4); break; }; };   

When I run:

  var te = new game (2); TE.player1.testFunction (); & Lt; --- Logger.log (TE.player.green);   

I get an error in the subject line.

Specifically:

You have to be able to call it from the outside The test function is not highlighted for.

It has to do this:

  function player () {this.green = 0; This.testfunction = function () {this.green = 99; }; };   

A better way would be:

  function player () {// Capital class name this.green = 0; }; Player.prototype.testFunction = function (num) {this.green = num || 99; }   

It does not need to examine all its examples of class class in this way because there is a priority instead of the property, prototype will have this property, which will be the prototype Inheritance.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -