0

I am learning how to create and write Javascript objects.

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;}

However, I am also learning that Javascript employs a 'prototype' to every created object. So game is not just a single object instance, it can also have game.prototype which is another object, but empty.

As far as I see it, both objects start off empty until you start filling it in with code. But my question is, why have prototype at all? Why not just write down code inside your object itself like above? What advantages does one get for using prototype over not using it?

Shawn
  • 1,175
  • 2
  • 11
  • 20
  • no. Your `game` value is a plain object. The value of `game.primaryStats` is a constructor function that can be used to create other objects. The prototype object for `game` is just `Object.prototype`. – Pointy Dec 24 '16 at 23:27
  • You can find a good explanation here: http://stackoverflow.com/questions/8433459/js-why-use-prototype – Davide Perozzi Dec 24 '16 at 23:30
  • Read this [book](http://shop.oreilly.com/product/mobile/9780596517748.do) – rckrd Dec 24 '16 at 23:31

0 Answers0