Sandboxing jQuery
Sandboxing jQuery
I know this is not strictly a jQuery question.
I am writing a module framework and I want to restrict modules from going outside of it's container. I have the following test code. Does anyone know a better method to identify the caller as an instance of Module. Using the constructor property prevents me from further chainig og objects (the contructor property of Sub sholud really be Sub not Module)
- <div id="test"><h2>hjkhkjh</h2></div>
- <h2>jhkjhjk</h2>
- <script type="text/javascript">
- function Module() {
- }
- function Sub () {
- console.log($('h2'));
- };
- Sub.prototype = new Module();
- Sub();
- console.log($('h2'));
- function $() {
- var args = Array.prototype.slice.call(arguments);
- var callerFunction = arguments.callee.caller;
- if (callerFunction && callerFunction.prototype.constructor == Module) {
- return window.jQuery(args[0], '#test');
- }
- return window.jQuery.apply(null,args);
- }
- </script>