0

I am working on a JavaScript application that let's other developers build their own apps on top of it. A framework will be provided for them.

My question:

How do I implement this framework in such a way that developers have access to certain functions or objects. For example, I would like to prevent access to certain properties of window object.

I thought about parsing their codes which I believe is a good solution. But I am not positive that it would work on obfuscated codes.

I would like some suggestions on how I should do it.

Kennedy
  • 2,146
  • 6
  • 31
  • 44
  • What exactly do you mean with 'window and parent object'? Why would you prevent one from using `window`? – pimvdb Oct 18 '11 at 09:02
  • if you prevent someone to use window - you prevent of using potentially everything, because global variables are in window namespace. I doubt this is a good idea. – dmitry Oct 18 '11 at 09:11
  • If possible, provide link to your existing solution so it's easier for people to give feedback based on that. :) You might want to consider using closures, though, as these allow you to hide certain properties. Just return the ones you want to expose. – Juho Vepsäläinen Oct 18 '11 at 09:11
  • I do not want developers to have direct access to some of the properties of window. For example, window.MyFramworkObject should not be accessible to developers. – Kennedy Oct 18 '11 at 09:19

1 Answers1

3

Your approach sounds flawed. You really don't want to be "fighting" with your developers by hiding things that they're used to accessing.

I would suggest that you try and provide a full (and well documented) API that encourages developers to use your framework in ways that you expect. Understand their use cases (how you expect them to use your platform) and provide APIs that make sticking to your "supported" objects a no-brainer.

On the general topic of encapsulation (e.g. making functions private) then there are various patterns that you can employ in Javascript. A book such as JavaScript Patterns would be a great reference for you. Alternatively, searching for "javascript private functions" should get you to things such as this...

JavaScript private methods

Community
  • 1
  • 1
Martin Peck
  • 11,440
  • 2
  • 42
  • 69