5 Replies - 342 Views - Last Post: 18 February 2013 - 06:09 AM

#1 jskidd3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 16-September 12

Using Prototype Functions

Posted 18 February 2013 - 05:23 AM

Hi. Below is a small piece of code that I can't get to work. It's actually a smaller version of the full thing, but it does a very similar thing and gives the same error.

The error I'm receiving is:

Uncaught TypeError: Object #<Player> has no method 'move' test.js:6
init test.js:6
(anonymous function)

I'd really appreciate it if somebody could let me know where I'm going wrong.

Thanks,
Joel.

var player;

init();

function init() {
	player = new Player();
	player.move();
}

function Player() {
	this.width = 10;
	this.height = 10;
}

Player.prototype.move = function() {
	alert("Hello, I've moved");
};



Is This A Good Question/Topic? 0
  • +

Replies To: Using Prototype Functions

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,333
  • Joined: 08-June 10

Re: Using Prototype Functions

Posted 18 February 2013 - 05:40 AM

why do you call the function before you even define it?

well that itself is not the problem due to function hoisting. but since the prototypal inheritance statement is not hoisted, the Player Constructor does not know the move() method on instantiation.
Was This Post Helpful? 0
  • +
  • -

#3 jskidd3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 16-September 12

Re: Using Prototype Functions

Posted 18 February 2013 - 05:43 AM

Ok sorry could you say that again but in much noobier language? I'm pretty new to Javascript and to be honest what you just said I didn't understand a word of :P
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,333
  • Joined: 08-June 10

Re: Using Prototype Functions

Posted 18 February 2013 - 05:49 AM

ok.

what you have written:
var player;

init();

function init() {
	player = new Player();
	player.move();
}

function Player() {
	this.width = 10;
	this.height = 10;
}

Player.prototype.move = function() {
	alert("Hello, I've moved");
};


how Java​Script actually executes the code:
var player;
// note how the function declarations have been put before the call to init()
function init() {
	player = new Player();
	player.move();
}

function Player() {
	this.width = 10;
	this.height = 10;
}

init();

Player.prototype.move = function() {
	alert("Hello, I've moved");
};

Was This Post Helpful? 1
  • +
  • -

#5 jskidd3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 16-September 12

Re: Using Prototype Functions

Posted 18 February 2013 - 05:52 AM

Much better, thanks very much! =)
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,333
  • Joined: 08-June 10

Re: Using Prototype Functions

Posted 18 February 2013 - 06:09 AM

if you want to do a more modern (and less error prone) approach, look into Object.create().

e.g.
function playerFactory() 
{
  var methods = {
    move: function move() {
      alert("Hello, I've moved");
    }
  };
  var props = {
    width: {
      value: 10,
      writable: true // or whatever options you like
    },
    height: {
      value: 10
      writable: true 
    }
  };
  return Object.create(methods, props);
}

var player = playerFactory();

This post has been edited by Dormilich: 18 February 2013 - 06:10 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1