Need help with script


#1
var Duane = function(){
	this.name = "duane";
	this.size = "medium";
	this.canFight = false;
	this.moves = {
		uppercut: "throws uppercut",
		spinkick: "does a spin kick",
		spit: "spits on you"
	};
	
}

Duane.prototype.Fight = function(){
	
	var randomMove = Math.floor((Math.random() * 3) + 1);
	if (randomMove === 1) {
		alert(this.moves.uppercut) // want to print this out in alert
return this.moves.uppercut;
} else if (randomMove === 2) {
	alert(this.moves.spinkick) // or print this out in alert
return this.moves.spinkick;
}
else {
	alert(this.moves.spit) // or print this out in alert
return this.moves.spit;
	}
};


var duane = new Duane();

alert(duane); // and here as alert

Need a hand with this code.

need it so that it prints out in alert when the script is called.
I’m still a beginner so need help with wrong logic syntax.

Thanks in Advance


#2

Fight is a method, in order for a method to execute, it needs to be called (just like functions need to be called in order to execute)


#3

Could you share a small example?


#4

objectInstance.methodName() is the general syntax for calling a method on a object instance. duane is your object instance in this case


#5

Tried that many times.

inspector says

Uncaught TypeError: Duane.Fight is not a function
    at script.js:30

#6

i got it to work successfully, you would need to show me what you have done


#7

Here is how I’v set up my index.html.

and here is the script

and here is the index.html in google chrome


#8

just show me the JS code, i want to see how you implement the method call, which doesn’t seem to be done right


#9
var Duane = function(){
	this.name = "duane";
	this.size = "medium";
	this.canFight = false;
	this.moves = {
		uppercut: "throws uppercut",
		spinkick: "does a spin kick",
		spit: "spits on you"
	};
	
}

Duane.prototype.Fight = function(){
	
	var randomMove = Math.floor((Math.random() * 3) + 1);
	if (randomMove === 1) {
		alert(this.moves.uppercut) // want to print this out in alert
return this.moves.uppercut;
} else if (randomMove === 2) {
	alert(this.moves.spinkick) // or print this out in alert
return this.moves.spinkick;
}
else {
	alert(this.moves.spit) // or print this out in alert
return this.moves.spit;
	}
};


Duane.Fight();

this is the code i used.


#10

why did you remove the instance you created here:

var duane = new Duane();

calling a method on a undefined variable will indeed cause an error.

Please answer me why you would remove it

You can’t call a method on the object itself, methods are always called on instances

objects are like blue prints, you can design a blue print of a door (object) but to actual open the door (method) you will first need to create an actual door (instance)


#11

Thanks that fixes it.

I’m not sure why i removed it, thought it wasn’t needed. (not to great with objects atm)

Yeah makes alot more sense now.


#12

Objects require some practice, i understand that :wink:

All good now, no questions left?


#13

That’s all for the moment, thanks


#14