0

Help me grasp this please. I want to create object, which property "visible" and method "hi", but getting error on line 2:

TypeError: Cannot set property 'visible' of undefined

in:

var NewFilter = {};
NewFilter.prototype.visible = false;
NewFilter.hi = function () { console.info("hi"); }

OK I know that I have to actually create that object, but why the hell it's property throwing error when it should eventually do upon object creation?

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Szymon Toda
  • 4,454
  • 11
  • 43
  • 62
  • Why are you setting `visible` on `prototype`? – thefourtheye Apr 30 '14 at 07:27
  • Object instances do not have a `.prototype` property, the constructor function that created the instance does. What prototype is and what it can be used for is explained here: http://stackoverflow.com/a/16063478/1641941 – HMR Apr 30 '14 at 08:59

1 Answers1

1

Empty objects don't have a prototype property, so you can't set a property on it's (nonexistent) prototype.

Just set it on the object itself, instead:

var NewFilter = {};
NewFilter.visible = false;
NewFilter.hi = function () { console.info("hi"); }

If you want to learn more about JavaScript prototypes, I'd suggest looking for a few tutorials / sites.

Community
  • 1
  • 1
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • @sebcap26: Oh, indeed. Copied that from the question, fixed both :-) I believe the privileges on "small" edits come with [2k rep](http://stackoverflow.com/help/privileges) – Cerbrus Apr 30 '14 at 07:36