-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgimme.js
53 lines (50 loc) · 1.68 KB
/
gimme.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Gimme! v1.0 - quickly borrow sandboxed native objects for safe, prototypal fun.
*
* MIT license
* author: Daniel Espeset <[email protected]>
* https://github.com/danielmendel/gimme
*
* example:
*
* var myString = gimme('String');
* var foo = new myString( 'bar' );
* var bar = 'hmm';
* foo instanceof String // false
* foo instanceof myString // true
*
* myString.prototype.hello = 'world!';
* foo.hello // 'world!'
* bar.hello // undefined
*
**/
!function( scope ){
function gimme( objectType ){
// generate our temporary global transport key
var key = '___gimmeSomethingNative';
// collision protection
while( window.hasOwnProperty(key) ){
key = '___gimmeSomethingNative' + Math.floor( 100 + Math.random() * 200 ) + ~new Date()
}
// inject iframe & assign the specified objectType to our transport key in this window.
var iframe = document.createElement("iframe")
iframe.style.display = "none"
iframe.id = key
document.body.appendChild(iframe)
frames[frames.length - 1].document.write(
"<script>parent."+key+" = "+objectType+";<\/script>"
);
// store the borrowed object
var borrowedObj = window[key]
// clean the global namespace
delete window[key]
return borrowedObj
}
// play nice
var namespaceBackup = scope.gimme
scope.gimme = gimme
scope.gimme.noConflict = function(){
scope.gimme = namespaceBackup
return gimme
}
}( window );