function cart(){
    this.recalc=function(num,money){
        alert()
};
    this.add=function(money,num){
     this.recalc(num,money)
}
}
类大概写成这个样子的,不知道是不是写的不对。用的时候使用
cart = new cart()
cart.add(100.00,1); 这样使用的,但是提示我recalc这句有问题提示不存在,是不是类写的有问题,我打算把所有操作cart的方法都写成一个类来调用。大家给看看。给点意见。好久不写JS都忘了怎么写了。

解决方案 »

  1.   

    function cart(){ 
    var obj=this;
    this.recalc=function(num,money){ alert() }; this.add=function(money,num){ obj.recalc(num,money) } } 
      

  2.   

    function cart(){
    var obj = this;//此时的this是cart
    this.recalc=function(num,money){
    alert()
    }
    this.add=function(money,num){
    this.recalc(num,money)//此时的this是add,所以要obj.recalc(num, money)
    }
    }
      

  3.   


        function cart(num, money) {
            this._num = num;
            this._money = money;
        };    cart.prototype = {
            recalc: function () { return this._num + this._money; },
            add: function () { return this.recalc(); }
        };    var c = new cart(1, 2);
        alert(c.recalc());
        alert(c.add());
      

  4.   

    http://www.cnblogs.com/jikey/archive/2011/05/13/2045005.html
      

  5.   

    http://www.cnblogs.com/jikey/archive/2011/05/13/2045005.html