Skip to content

Comment on Advanced objects in JavaScriptparent

Comments

I like that, but you'd still have to call a function to get the properties though and this wouldn't prevent anyone from accessing those properties directly.

Like:

  obj.setFoo("bar");
  obj.getFoo();
  obj.foo = "bad stuff"; //overwritten
instead of
  obj.foo = "bar";
  obj.foo;
  obj.foo_ //error, not allowed access

True enough, and if privacy is a must have, my implementation is a non-starter.

I tend towards the (somewhat unpopular and certain arguable) opinion that need for privacy via a runtime-enforced mechanism is overrated... give us developers using an API/library effective/well-documented methods that do what we need and warn us when we shouldn't touch something with a convention (like an underscore), and usually we're happy to leave the black box closed.

It's usually when abstractions leak or implementations aren't complete that we're tempted to tinker across boundaries, and when that happens, the lack of a privacy enforcement mechanism may not be the real problem.

(OTOH, they can indeed help keep some bad situations from getting worse, and when trusted code is mingling with untrusted in situations like web mashups, privacy-enforcement can be really helpful for security...)

I don't know why, because I haven't thought through it enough yet but I really like the idea of freezing or protection in the case of a reactive data model like Backbone models. I'm thinking of maybe writing up some quick implementation where you provide a schema and you get a reactive object that is frozen and has a catch all. Just want to see where that goes.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.