Advertisement
Guest User

Untitled

a guest
Dec 29th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.65 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3.     <head>
  4.         <title>Test WebIDL conversion</title>
  5.         <script>
  6. /**
  7.  * Exports a function to the global scope in a way that conforms to Web IDL.
  8.  *
  9.  * These objects are indistiguishable from host objects. That is, you can create
  10.  * objects like the ones that are exposed by the browser, like window.Event,
  11.  * window.Node, etc.
  12.  *
  13.  * Copyright (c) 2012 Marcos Caceres
  14.  * Licensed under the MIT license.
  15.  *
  16.  * Example:
  17. (function(){
  18.     var fooInstance;
  19.    
  20.     // your secret constructor
  21.     function Foo(){}
  22.    
  23.     //A test function
  24.     Foo.prototype.test = function(){
  25.        if (!(this instanceof Foo)) {
  26.             throw new TypeError("Illegal invocation");
  27.         }
  28.         console.log("wee!")
  29.     }
  30.     //Add it to the window object
  31.     exportPublicConstructor(Foo);
  32.     //real instance
  33.     fooInstance = new Foo();
  34.     fooInstance.test();
  35. }());
  36.  
  37.  
  38. //proof
  39. console.log(window.Foo);
  40. //Following all throw
  41. Foo();
  42. new Foo();
  43. Foo.prototype.test();
  44. **/
  45.  
  46. function exportPublicConstructor(privateConstructor) {
  47.    
  48.     //emulate native code toString()
  49.     function toStringMaker(name) {
  50.         return function toString() {
  51.             return 'function ' + name + '() { [native code] }';
  52.         };
  53.     }
  54.    
  55.     // create the public contructor
  56.     var identifier = privateConstructor.name || 'DOMObject',
  57.         publicConstructor = eval('(function ' + identifier + '(){return new privateConstructor()})');
  58.    
  59.    
  60.     // the new constructor should have the same prototype
  61.     try {
  62.         Object.defineProperty(publicConstructor, 'prototype', {
  63.             writable: false,
  64.             enumerable: true,
  65.             configurable: false,
  66.             value: privateConstructor.prototype
  67.         });
  68.     } catch(ex) {
  69.         publicConstructor.prototype=privateConstructor.prototype;
  70.     }
  71.    
  72.     // the functions should not leak their implementation
  73.     privateConstructor.toString = publicConstructor.toString = toStringMaker(identifier);
  74.     privateConstructor.prototype.constructor=publicConstructor;
  75.     for (var i in privateConstructor.prototype) {
  76.         if (privateConstructor.prototype.hasOwnProperty(i)) {
  77.             privateConstructor.prototype[i].toString = toStringMaker(i);
  78.         }
  79.     }
  80.  
  81.     // expose the interface on the global object
  82.     Object.defineProperty(window, identifier, {
  83.         value: publicConstructor
  84.     });
  85. }
  86.         </script>
  87.     </head>
  88.     <body>
  89.         <script>
  90. (function(){
  91.     var fooInstance;
  92.    
  93.     // your secret constructor
  94.     function Foo(){ this.date=new Date(); }
  95.     Foo.name="Foo" //IE fix
  96.     function FooPrototype() { }
  97.     Foo.prototype=new FooPrototype();
  98.     Foo.prototype.toString=function() { if(this instanceof Foo) { return '[object Foo]'; } else { return '[object FooPrototype]'; } }
  99.    
  100.     //A test function
  101.     Foo.prototype.test = function(){
  102.        if (!(this instanceof Foo)) {
  103.             throw new TypeError("Illegal invocation");
  104.         }
  105.         console.log("test")
  106.     }
  107.    
  108.     //Add it to the window object
  109.     exportPublicConstructor(Foo);
  110.    
  111.     //real instance (test)
  112.     fooInstance = new Foo();
  113.     fooInstance.test();
  114.    
  115. }());
  116.  
  117. //proof
  118. console.log(window.Foo);
  119.  
  120. //check instance prototype chain
  121. var fooInstance = new Foo();
  122. Foo.prototype.test2=function(){console.log('test2')};
  123. fooInstance.test2();
  124.  
  125. //Following all throw
  126. if(!window.setImmediate) { window.setImmediate=function(f) { setTimeout(f, 0); }; }
  127. setImmediate(function() { console.log(Foo() instanceof Foo); });
  128. setImmediate(function() { console.log(new Foo() instanceof Foo); });
  129. setImmediate(function() { console.log(new Foo().constructor==Foo); });
  130. setImmediate(function() { console.log(fooInstance=="[object Foo]"); });
  131. setImmediate(function() { console.log(Foo.prototype=="[object FooPrototype]"); });
  132. setImmediate(function() { try { Foo.prototype.test(); console.log(false); } catch (ex) { console.log(true); } });
  133.  
  134.         </script>
  135.     </body>
  136. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement