Javascript对象与Javascript原型?

时间:2016-12-24 23:25:50

标签: javascript oop object prototype

我正在学习如何创建和编写Javascript对象。

var game = {    primaryStats: function (level, hp, hpcap, balance, balancecap, exp){
        this.level = level;
        this.hp = hp;
        this.hpcap = hpcap;
        this.balance = balance;
        this.balancecap = balancecap;
        this.exp = exp;}

但是,我也知道Javascript使用的是'原型'到每个创建的对象。所以game不仅仅是一个对象实例,它还可以有game.prototype,它是另一个对象,但是为空。

据我所知,两个对象都开始为空,直到你开始用代码填充它。但我的问题是,为什么要有原型?为什么不像上面那样在对象内部写下代码呢?使用原型比不使用原型有什么好处?

0 个答案:

没有答案
相关问题