这是创建对象并在其中放置函数的正确方法吗?我遇到的每个示例都使用易于理解和在实际情况中有效使用的代码。所以我试图通过这样做来解决这个问题。
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;
document.write(calc.arithmetic(input)) //calling the method in the context of the object.