gpt4 book ai didi

javascript - 在函数中插入输入值作为参数不起作用

转载 作者:行者123 更新时间:2023-12-02 15:29:27 27 4
gpt4 key购买 nike

我试图将从表单中获取的值传递到名为 Robot() 的构造函数中;我可以通过自己传递参数在控制台中完成此任务。例如: var travis = new Robot("Travis","Blue","medium");如果我输入 travis.speed 之类的内容,它会按预期返回 1。

但是当我尝试使用网站上的表单来执行此操作并进行测试时,控制台显示 Travis 未定义。

我觉得我做的一切都是正确的,但显然我不是。如果有人可以帮助我,我将非常感激。

我的 script.js:

var $canvas = $("canvas");
var ctx = $canvas[0].getContext("2d");

function Robot(name, color, robotBuild){
this.name = name.toLowerCase();
this.robotBuild = 'medium';
this.health = 100;
this.speed = 1;
this.strength = 1;
this.powerState = false;
this.maxStrength = 100;
this.minStrength = 1;
this.maxSpeed = 10;
this.minSpeed = 1;
this.maxHealth = 100;
this.minHealth = 0;
this.points = 0;
//ROBOT POSITION VALUES
this.posX = 0;
this.posY = 0;
//ROBOT PHYSICAL PROPERTIES
this.robotWidth = 10;
this.robotHeight = 10;
this.robotColor = color.toLowerCase();
}
Robot.prototype.draw = function(){
ctx.beginPath();
ctx.rect(this.posX, this.posY, this.robotWidth, this.robotHeight);
ctx.fillStyle = this.robotColor;
ctx.fill();
ctx.closePath();
this.powerState = true;
};
Robot.prototype.power = function(powerState){
powerState.toLowerCase();
if(powerState === "on"){
this.powerState = true;
}else if(powerState === "off") {
this.killRobot();
}else {
console.log("Input must be a value of 'on' or 'off'.");
}
};

Robot.prototype.killRobot = function(){
this.powerState = false;
ctx.clearRect(0, 0, $canvas.width, $canvas.height);
console.log(this.name + " the robot is now dead.");
};

Robot.prototype.setStrength = function(num){
if( num < this.minStrength || num > this.maxStrength ){
return 'error: strengthLevel can not be less than 0 or greater than 100.';
}else if(isNaN(num)){
return 'error: Input must be a numbered value.';
}else {
this.strength = num;
return this.strength;
}
};
Robot.prototype.incHealth = function(num){
if(isNaN(num)){
console.log('error: Input must be a numbered value.');
}
else if((this.health + num) >= this.maxHealth ){
this.health = 100;
console.log('You have reached full Health, use it wisely.');
}else {
this.health += num;
console.log('Your Health is increasing.');
return this.health;
}
};

Robot.prototype.decHealth = function(num){
if(isNaN(num)){
console.log('error: Input must be a numbered value.');
}
else if( (this.health - num) <= this.minHealth){
this.powerState = false;
this.health = 0;
this.killRobot();
return this.health;
}else {
this.health -= num;
console.log('You are loosing health:(');
return this.health;
}
};

Robot.prototype.incSpeed = function(num){
if( (this.speed + num) < this.minSpeed || (this.speed + num) > this.maxSpeed ){
console.log('error: Speed Level can not be less than 0 or greater than 100.');

}else if(isNaN(num)){
console.log('error: Input must be a numbered value.');

}else {
this.speed += num;
return this.speed;
}
};

Robot.prototype.decSpeed = function(num){
if( (this.speed - num) < this.minSpeed || (this.speed - num) > this.maxSpeed ){
console.log('error: Speed Level can not be less than 0 or greater than 100.');

}else if(isNaN(num)){
console.log('error: Input must be a numbered value.');

}else {
this.speed -= num;
return this.speed;
}
};

Robot.prototype.incStrength = function(num){
if( (this.strength + num) < this.minStrength || (this.strength + num) > this.maxStrength ){
console.log('error: strengthLevel can not be less than 0 or greater than 100.');

}else if(isNaN(num)){
console.log('error: Input must be a numbered value.');

}else {
this.strength += num;
return this.strength;
}
};

Robot.prototype.decStrength = function(num){
if( (this.strength - num) < this.minStrength || (this.strength - num) > this.maxStrength ){
console.log('error: strengthLevel can not be less than 0 or greater than 100.');

}else if(isNaN(num)){
console.log('error: Input must be a numbered value.');

}else {
this.strength -= num;
return this.strength;
}
};

//MOVING ROBOT POSITION FUNCTIONALITY
Robot.prototype.position = function(){
this.position = {
posX: this.posX,
posY: this.posY
};
return this.position;
};

Robot.prototype.moveUp = function(y){
if(this.powerState === true){
this.posY += y;
return this.posY;
}else {
console.log("Robot is dead and can not move.");
}

};

$(".createRobot").click(function(){
$("#robotForm").toggle();
});

$("#submitRobot").click(function(e){
e.preventDefault();
var robotName = $("#robotName").val();
var robotColor = $("#robotColor").val();
var robotBuild = $("#robotBuild option:selected").text();
console.log(typeof robotName); // should return "string"
console.log(robotName + ", " + robotColor + ", " + robotBuild);
var robot = new Robot(robotName, robotColor, robotBuild);
});

HTML 表单:

<div id="robotForm">
<h4>Create your robot with the form below:</h4>
<p><label>Your Robot's name:<input class="robotName" type="text" name="name" value="" placeholder="Robot's name"></label></p>
<p><label>Your Robot's color:<input class="robotColor" type="text" name="color" value="" placeholder="Robot's color"></label></p>
<p><label>Your Robot's build type:
<select class="robotBuild" name="robotBuild">
<span>Select your robot's build type</span>
<option name="small" value="small">Small Robot Build</option>
<option name="medium" value="medium">Medium Robot Build</option>
<option name="large" value="large">Large Robot Build</option>
</select>
</label>
</p>
<button class="submitRobot" type="submit">Submit your Robot</button>
</div>

最佳答案

我不知道你想用返回做什么,但我认为这并不重要。

您必须确保包含 Robot 类的代码在事件处理程序执行之前执行(即,在 jQuery 之外的任何其他文件之前加载 JavaScript 文件)。

此外,Robot 的范围不正确。我不再使用这种类型的声明,因为它可以执行所有操作,但不是您想要的。相反,最好直接将类分配给窗口对象,方法是:

window.Robot = function(...) { ... };

这样,Robot 类就可以全局使用。

法比安

关于javascript - 在函数中插入输入值作为参数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33443920/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com