Jump to content

Objects In Javascript


Nohana

Recommended Posts

Hello,For a few hours now, I've been fiddling with my script to call a method, but the painful truth is that I don't know to create one, haha. I thought of doing things like one would do in Java but that doesn't seem to cut here in JS. Here's my code:

function Player(){	function moveLeft()	{		alert("hidari!");	}}

And:

Player.moveLeft();

Technically, what this should do is pop up an alert box saying hidari. Needless to say, doesn't work. The way I'm handling the object (Player in this case) is obviously wrong, but I don't have a clue about how to to put things right. I googled for "javascript method" and "javascript classes" but found no relevant results. On another note, I've tried also creating an object player as var player = new Player(); instead of calling the method directly from the function, but that didn't work either.Could someone please shed some light on how to properly design/create objects in Javascript and specifically call methods? How can I call the method "moveLeft()" in my code?Thanks. :)

Link to comment
Share on other sites

The arguments are optional (even passing them is optional) but I figured you'd want to see the whole thing.

<script type="text/javascript">	function Player (lev, spec, wep) {		this.level = lev || 1;		this.species = spec || "human";		this.weapon = wep || "sword";		this.locat = [0,0];		this.moveLeft = function () {			alert (this.species); // an example		}		}	P = new Player(3, "vampire");</script>---<button onclick="P.moveLeft();">Kind?</button>

Link to comment
Share on other sites

Thanks, it works now. :)One last question, is there no way to call the method directly from the function as in a static object, that it is, without having to ever create an object and store it in a variable? Similar to what you have with Math. Math.random is a method of Math, but it's not necessary to store Math in a variable at all.

Link to comment
Share on other sites

There is no scope resolution operator in JS (just as there are really no such things as classes). The Math class is really I suppose just a variable that is set by default...

Link to comment
Share on other sites

What an odd mixture JS has:

  1. methods attached to pseudo-objects, like Math.random()
  2. methods attached "real" objects, like number.toFixed()
  3. stand-alone functions, like parseInt()
  4. user-defined functions, which can be either

And I'm not even counting DOM object methods. I actually like the flexibility, but then I've learned the language as it has developed. Learning it cold must be weird.

Link to comment
Share on other sites

Well, all procedural languages with OO tacked on the end are like that, really...

<?php	class Main {		function __construct() {			echo "Hello world";		}	}		$main = new Main();?>

Link to comment
Share on other sites

Thanks, it works now. :) One last question, is there no way to call the method directly from the function as in a static object, that it is, without having to ever create an object and store it in a variable? Similar to what you have with Math. Math.random is a method of Math, but it's not necessary to store Math in a variable at all.
Math is an object that has simply already been populated, or constructed before runtime.also consider how var mr = Math.random; mr();mr();mr();mr();mr(); //is a lot faster to type and execute thanMath.random();Math.random();Math.random();Math.random();Math.random();some times it is necessary to store a math method because you need the performance.you cannot call the method this.moveLeft beacuse the is no this, and for the function, no "this.species"...if you want an object assembled at runtime, like Math, simply code a JSON-like structure with your methods as object properties.the functions can then be called individually, and you don't need to construct an instance to use them.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...