对象&原型

时间:2016-08-31 05:09:30

标签: javascript jquery object prototype

javascript/jQuery File

这是我正在使用的代码。当newOrder设置为:

时,它可以工作
public class MainActivity extends AppCompatActivity {

    Context context;
    private SharedPreferences mPrefs;
    private CountDownTimer countDownTimer;
    // here is prefs key
    String Key = "TIME";
    long time = 30000; // 30 sec
    EditText number;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        number = (EditText) findViewById(R.id.foodName);
        mPrefs = this.getSharedPreferences("myAppPrefs", Context.MODE_PRIVATE);
        if (mPrefs.getLong(Key, 0) == 0) {
            mPrefs.edit().putLong(Key, time).apply();
        } else {
            time = mPrefs.getLong(Key, time);
        }
        Log.d("App Open Time ", time + "");
        startCount();


    }

    private void startCount() {
        countDownTimer = new CountDownTimer(time, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mPrefs.edit().putLong(Key, millisUntilFinished).apply();
                number.setText(millisUntilFinished/1000 + "");
                Log.d("Remaining time", millisUntilFinished/1000 + "");
            }

            @Override
            public void onFinish() {
                Log.d("finished  time", time + "");
                mPrefs.edit().putLong(Key, 0).apply();
                number.setText(time + "");
            }
        };
        countDownTimer.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        countDownTimer.cancel();
        Log.d("App in background", "with " + time);
    }
}

但每当我尝试调用原型时,它都会在JS控制台中声明它未定义。

我对这种材料还是比较新的。记住这一点。

我尝试的是添加浇头和大小,并在我的输出中使用原型/对象组合。

感谢任何帮助!

一个有抱负的编码员。

3 个答案:

答案 0 :(得分:0)

我认为您的PIZZA函数需要三个(topping,size,price)参数,但是您传递的单个参数使剩余的2个参数(size, price)无法发现。

function Pizza(topping, size, price) {
  this.topping=topping;
  this.size=size;
  this.price=price;
}

为此构造函数传递三个参数。

答案 1 :(得分:0)

您的Pizza构造函数有三个参数:

function Pizza(topping, size, price) {
  // ...
}

...但你只传递了一个论点:

new Pizza(toppingsTotal + inputtedSize);

因此,披萨的this.topping将是toppingsTotalinputtedSize的总和,其余的将是undefined

确保您的参数与您的参数一致,否则Pizza将无法按预期运行。

答案 2 :(得分:-1)

代替newOrder.price() Pizza.pizzaPrice(),以便在致电newOrder时引用对象Pizz.prototype.pizzaPrice()

但请注意,您尚未将sizeprice参数传递给var newOrder = new Pizza(toppingsTotal + inputtedSize);并且您正在调用.price(),其中函数名称为pizzaPrice

在下面的stacksnippets中,三个参数传递给new Pizza()newOrder.pizzaPrice()用于调用Pizza.prototype.pizzaPrice

function Pizza(topping, size, price) {
  this.topping = topping;
  this.size = size;
  this.price = price;
}

Pizza.prototype.pizzaPrice = function() {
  return this.topping + this.size
}

var newOrder = new Pizza("cheese", 12, 10.00);

console.log(newOrder.pizzaPrice());