在JavaScript中创建一个对象,使用从文本框中检索的值进行数学计算?

在JavaScript中创建一个对象,使用从文本框中检索的值进行数学计算?,javascript,function,object,calculator,Javascript,Function,Object,Calculator,我想用物体做一个计算器。以下是JSFIDLE: 我使用这段代码是为了计算数字,而我制作了计算器的其余部分: /*Computes the values inside the box and returns the answer.*/ function compute(form) { form.display.value = eval(form.display.value); } 现在我需要创建一个进行计算的对象,而不是使用JavaScript预定义的对象eval。我是这样想的

我想用物体做一个计算器。以下是JSFIDLE:

我使用这段代码是为了计算数字,而我制作了计算器的其余部分:

/*Computes the values inside the box and returns the answer.*/
function compute(form)  
{
    form.display.value = eval(form.display.value); 
}  
现在我需要创建一个进行计算的对象,而不是使用JavaScript预定义的对象eval。我是这样想的:

基本上,该对象从id为“input”的文本框中获取值。值的形式为2*3、1+2、5/7等。。。所以我在想,如果我用substr提取字符串中的第二段数据,我可以检查它是乘、除、减还是加

    function calc(arithmetic)
{
    this.arithmetic = arithmetic;
}        

var myCalc = new calc(input)
    {
    function maths (input) 
{
    var str = input;
    var a=str.substr(1,1); // example: 1+2   var a holds the 1
    var b=str.substr(2,1); //                var b holds the "+"
    var c=str.substr(3,1); //                var c holds the 2
                           // if the sign is equal to "+" add var a plus var b
                           // and then place them inside answerNumber.
                           // then take answerNumber and place its value
                           // inside the page element with the id "input"
    if(b == "+")
    {
        answerNumber = a + c;
    }
    else if(b == "-")
    {
        answerNumber = a + c;
    }
    else if(b == "*")
    {
        answerNumber = a * c;
    }
    else if(b == "/")
    {
        answerNumber = a / c;
    }
    document.getElementById("input").value=answerNumber;
        }


    }       
我尝试在各种网站上阅读有关对象的内容,但它们根本没有帮助,以下是我去过的一些地方:http://www.crockford.com/javascript/private.html" , "http://www.w3schools.com/js/js_objects.asp“在其他地方。因为我想做的这些似乎不是很有用。也许你们中的一些人可以帮忙

以下是将触发对象函数并将其放入的按钮和文本框:

<input type="button" value="   =    " name="enter" onClick="compute(this.form)">
<input name="display"  id="input" size=25>

下面是我的全部代码,包括外部JavaScript

HTML:


任务2
计算器

文件。书写(“Mallery,Cody”);





说明:
单击一个数字,然后单击一个操作,然后单击另一个数字,然后单击“=”按钮。
准备重新开始时按“C”。
“N”使您以前的数字为负数。
“L”按钮要求一个数字可用,
它将通过“那个数字”循环到“十加那个数字”
和添加所有值并显示。

例如1+2+3+4+5+6+7+8+9+10=55

导航器: txt=“浏览器代码名:”+navigator.appCodeName+“

”; txt+=“浏览器名称:”+navigator.appName+“

”; txt+=“浏览器版本:”+navigator.appVersion+“

”; txt+=“Cookies已启用:”+navigator.cookieEnabled+“

”; txt+=“平台:“+navigator.Platform+”

”; txt+=“用户代理标题:“+navigator.userAgent+”

”; document.getElementById(“Navigator”).innerHTML=txt;
外部JavaScript:

/* gets the input from the keyboard OR clicking the button */
function addChar(input, character) 
{
if(input.value == null || input.value == "0")
    input.value = character;
else
    input.value += character;
}


/*changes the sign of the number inside the input box from negative to positive.*/
function changeSign(input) 
{
if(input.value.substring(0, 1) == "-")
    input.value = input.value.substring(1, input.value.length);
else
    input.value = "-" + input.value;
}


/*Computes the values inside the box and returns the answer.*/
function compute(form)  
{
    form.display.value = eval(form.display.value); /*The Calculator uses an Object for all calculations*/
}                


/* Loops the input by the value + 10 */
function Loop(input)
{
     var num = parseInt(input) + 10;
     var i=0;
     var sum=0;

    while(i < num)
        {
            sum=sum+i;
            i++;
        }
    document.getElementById("input").value=sum;

}

/*Compute Arithmetic*/
function calc() //creating an empty object named calc
{
}

var calc = new calc(); //creating a new instance of the calc object

calc.arithmetic = function maths (input) 
{
    var str = input;
    var a=str.substr(1,1); 
    var b=str.substr(2,1);
    var c=str.substr(3,1);

    if(b == "+")
    {
        answerNumber = a + c;
    }
    else if(b == "-")
    {
        answerNumber = a + c;
    }
    else if(b == "*")
    {
        answerNumber = a * c;
    }
    else if(b == "/")
    {
        answerNumber = a / c;
    }
    document.getElementById("input").value=answerNumber;
}
/*从键盘或单击按钮获取输入*/
函数addChar(输入,字符)
{
if(input.value==null | | input.value==“0”)
input.value=字符;
其他的
input.value+=字符;
}
/*将输入框内数字的符号从负数更改为正数*/
功能更改符号(输入)
{
if(输入值子字符串(0,1)==“-”)
input.value=input.value.substring(1,input.value.length);
其他的
input.value=“-”+input.value;
}
/*计算框内的值并返回答案*/
函数计算(表格)
{
form.display.value=eval(form.display.value);/*计算器使用一个对象进行所有计算*/
}                
/*按值+10循环输入*/
功能循环(输入)
{
var num=parseInt(输入)+10;
var i=0;
var总和=0;
while(i

因此,您能为我指出正确方向的任何方法都是非常感谢的。

我不知道您为什么认为您需要一件物品。把它们放在一个函数中——你已经有了
数学
函数了。它周围的代码只是一个语法错误

通常,通过
var myCalc=新计算(输入)
您创建了
calc
构造函数的实例-它后面不应该跟函数体。还有,通过做

function calc() {}
var calc = new calc();
您只需用它的实例覆盖
calc
函数-非常奇怪

如果你想给你的函数命名,例如把它们作为属性放到一个对象上(这是个好主意),你应该使用一个简单的
object
literal。不需要创建多个实例的构造函数,您只需要一个:

var calc = {
   arithmetic: function maths (input) { … }
};
// then call
calc.arithmetic("1+3");
// which sets the input field to "4"

不要使用eval()!!!看起来您正在使用用户输入的值。不要那样做。我知道我需要创建一个对象来替换Eval()函数,这就是我的问题所在。我使用用户输入的值,因为这是一个计算器,需要1+2、3*4、5/6、3-2等。我需要找到一种不使用Eval的方法,并创建自己的对象。谢谢。我觉得你把事情太复杂了……怎么会这样?这是我唯一能想到的方法。我只是不能为它编写代码,因为我没有经验。@CodyGallery你正在寻找类似的东西。我之所以需要一个对象,是因为它是一个任务,我必须按照指示去做,即使这意味着使用一些我将要重新发明轮子的东西。谢谢你提供的信息!这很有帮助。我想你可以说我这么做的原因是为了了解物体。
var calc = {
   arithmetic: function maths (input) { … }
};
// then call
calc.arithmetic("1+3");
// which sets the input field to "4"