Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html>
- <head>
- <title>Test WebIDL conversion</title>
- <script>
- /**
- * Exports a function to the global scope in a way that conforms to Web IDL.
- *
- * These objects are indistiguishable from host objects. That is, you can create
- * objects like the ones that are exposed by the browser, like window.Event,
- * window.Node, etc.
- *
- * Copyright (c) 2012 Marcos Caceres
- * Licensed under the MIT license.
- *
- * Example:
- (function(){
- var fooInstance;
- // your secret constructor
- function Foo(){}
- //A test function
- Foo.prototype.test = function(){
- if (!(this instanceof Foo)) {
- throw new TypeError("Illegal invocation");
- }
- console.log("wee!")
- }
- //Add it to the window object
- exportPublicConstructor(Foo);
- //real instance
- fooInstance = new Foo();
- fooInstance.test();
- }());
- //proof
- console.log(window.Foo);
- //Following all throw
- Foo();
- new Foo();
- Foo.prototype.test();
- **/
- function exportPublicConstructor(privateConstructor) {
- //emulate native code toString()
- function toStringMaker(name) {
- return function toString() {
- return 'function ' + name + '() { [native code] }';
- };
- }
- // create the public contructor
- var identifier = privateConstructor.name || 'DOMObject',
- publicConstructor = eval('(function ' + identifier + '(){return new privateConstructor()})');
- // the new constructor should have the same prototype
- try {
- Object.defineProperty(publicConstructor, 'prototype', {
- writable: false,
- enumerable: true,
- configurable: false,
- value: privateConstructor.prototype
- });
- } catch(ex) {
- publicConstructor.prototype=privateConstructor.prototype;
- }
- // the functions should not leak their implementation
- privateConstructor.toString = publicConstructor.toString = toStringMaker(identifier);
- privateConstructor.prototype.constructor=publicConstructor;
- for (var i in privateConstructor.prototype) {
- if (privateConstructor.prototype.hasOwnProperty(i)) {
- privateConstructor.prototype[i].toString = toStringMaker(i);
- }
- }
- // expose the interface on the global object
- Object.defineProperty(window, identifier, {
- value: publicConstructor
- });
- }
- </script>
- </head>
- <body>
- <script>
- (function(){
- var fooInstance;
- // your secret constructor
- function Foo(){ this.date=new Date(); }
- Foo.name="Foo" //IE fix
- function FooPrototype() { }
- Foo.prototype=new FooPrototype();
- Foo.prototype.toString=function() { if(this instanceof Foo) { return '[object Foo]'; } else { return '[object FooPrototype]'; } }
- //A test function
- Foo.prototype.test = function(){
- if (!(this instanceof Foo)) {
- throw new TypeError("Illegal invocation");
- }
- console.log("test")
- }
- //Add it to the window object
- exportPublicConstructor(Foo);
- //real instance (test)
- fooInstance = new Foo();
- fooInstance.test();
- }());
- //proof
- console.log(window.Foo);
- //check instance prototype chain
- var fooInstance = new Foo();
- Foo.prototype.test2=function(){console.log('test2')};
- fooInstance.test2();
- //Following all throw
- if(!window.setImmediate) { window.setImmediate=function(f) { setTimeout(f, 0); }; }
- setImmediate(function() { console.log(Foo() instanceof Foo); });
- setImmediate(function() { console.log(new Foo() instanceof Foo); });
- setImmediate(function() { console.log(new Foo().constructor==Foo); });
- setImmediate(function() { console.log(fooInstance=="[object Foo]"); });
- setImmediate(function() { console.log(Foo.prototype=="[object FooPrototype]"); });
- setImmediate(function() { try { Foo.prototype.test(); console.log(false); } catch (ex) { console.log(true); } });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement