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?

2
  • Why are you setting visible on prototype? Commented Apr 30, 2014 at 7: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: stackoverflow.com/a/16063478/1641941 Commented Apr 30, 2014 at 8:59

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@sebcap26: Oh, indeed. Copied that from the question, fixed both :-) I believe the privileges on "small" edits come with 2k rep

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.