0

これは、オブジェクトを作成してその中に関数を配置する正しい方法ですか? 私が遭遇するすべての例は、理解しやすく、実際の状況で効果的に使用できるコードを使用しています。だから私はこれを理解しようとしています。

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.
4

2 に答える 2

0

Douglas Crockford のサイト (http://www.crockford.com/javascript/private.html) を見てみましょう。

于 2012-10-14T11:14:53.490 に答える
0

ここでは、myArithmetic のみを含む新しいオブジェクトで calc を上書きしています。

代わりに行う必要がありますcalc.myArithmetic=function(){};-これにより、関数を値として新しいプロパティが追加されます。これはおそらく必要なものです。

于 2012-10-14T11:10:14.563 に答える