Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. var F = function () {};
  2. var myObj = new F();
  3.  
  4. console.log(myObj);
  5.  
  6. getMutableCopy : function (object) {
  7. var f = function () {};
  8. f.prototype = object;
  9. return new f();
  10. }
  11.  
  12. > function foo {}
  13. > foo.name
  14. "foo"
  15. > foo.name = "bar"
  16. "bar"
  17. > foo.name
  18. "foo" // <-- looks like it is read only
  19.  
  20. function copy(parent, name){
  21. name = typeof name==='undefined'?'Foobar':name;
  22. var f = eval('function '+name+'(){};'+name);
  23. f.prototype = parent;
  24. return new f();
  25. }
  26.  
  27. var parent = {a:50};
  28. var child = copy(parent, 'MyName');
  29. console.log(child); // Shows 'MyName' in Chrome console.
  30.  
  31. function Cache(fallback){
  32. var cache = {};
  33.  
  34. this.get = function(id){
  35. if (!cache.hasOwnProperty(id)){
  36. cache[id] = fallback.apply(null, Array.prototype.slice.call(arguments, 1));
  37. }
  38. return cache[id];
  39. }
  40. }
  41.  
  42. var copy = (function(){
  43. var cache = new Cache(createPrototypedFunction);
  44.  
  45. function createPrototypedFunction(parent, name){
  46. var f = eval('function '+name+'(){};'+name);
  47. f.prototype = parent;
  48. return f;
  49. }
  50.  
  51. return function(parent, name){
  52. return new (cache.get(name, parent, typeof name==='undefined'?'Foobar':name));
  53. };
  54. })();
  55.  
  56. my_class = function () {}
  57. my_class.prototype.toString = function () { return 'Name of Class'; }
  58.  
  59. a = new my_class()
  60. a.does_not_exist()
  61.  
  62. /**
  63. * JavaScript Rename Function
  64. * @author Nate Ferrero
  65. * @license Public Domain
  66. * @date Apr 5th, 2014
  67. */
  68. var renameFunction = function (name, fn) {
  69. return (new Function("return function (call) { return function " + name +
  70. " () { return call(this, arguments) }; };")())(Function.apply.bind(fn));
  71. };
  72.  
  73. /**
  74. * Test Code
  75. */
  76. var cls = renameFunction('Book', function (title) {
  77. this.title = title;
  78. });
  79.  
  80. new cls('One Flew to Kill a Mockingbird');
  81.  
  82. Book {title: "One Flew to Kill a Mockingbird"}
  83.  
  84. var name ="bar";
  85. window["foo"+name] = "bam!";
  86. foobar; // "bam!"
  87.  
  88. function getmc (object, name) {
  89.  
  90. window[name] = function () {};
  91. window[name].prototype = object;
  92. return new window[name]();
  93.  
  94. }
  95.  
  96. foo = function(){};
  97. foobar = getmc(foo, "bar");
  98. foobar; // ▶ window
  99. foobar.name; // foo
  100. x = new bar; x.name; // foo .. not even nija'ing the parameter works
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement