Guest User

Untitled

a guest
Apr 25th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. function Something(){
  2. // init stuff
  3. }
  4. function createSomething(){
  5. return new Something.apply(null, arguments);
  6. }
  7. var s = createSomething(a,b,c); // 's' is an instance of Something
  8.  
  9. var createSomething = (function() {
  10. function F(args) {
  11. return Something.apply(this, args);
  12. }
  13. F.prototype = Something.prototype;
  14.  
  15. return function() {
  16. return new F(arguments);
  17. }
  18. })();
  19.  
  20. function construct(constructor, args) {
  21. function F() {
  22. return constructor.apply(this, args);
  23. }
  24. F.prototype = constructor.prototype;
  25. return new F();
  26. }
  27.  
  28. var createSomething = (function() {
  29. function F(args) {
  30. return Something.apply(this, args);
  31. }
  32. F.prototype = Something.prototype;
  33.  
  34. return function(args) {
  35. return new F(args);
  36. }
  37. })();
  38.  
  39. function testNew() {
  40. if (!( this instanceof arguments.callee ))
  41. return arguments.callee.apply( new arguments.callee(), arguments );
  42. this.arg = Array.prototype.slice.call( arguments );
  43. return this;
  44. }
  45.  
  46. testNew.prototype.addThem = function() {
  47. var newVal = 0,
  48. i = 0;
  49. for ( ; i < this.arg.length; i++ ) {
  50. newVal += this.arg[i];
  51. }
  52. return newVal;
  53. }
  54.  
  55. testNew( 4, 8 ) === { arg : [ 4, 8 ] };
  56. testNew( 1, 2, 3, 4, 5 ).addThem() === 15;
  57.  
  58. function Something() {
  59. // init stuff
  60. return this;
  61. }
  62. function createSomething() {
  63. return Something.apply( new Something(), arguments );
  64. }
  65. var s = createSomething( a, b, c );
  66.  
  67. var applyCtor = function(){
  68. var tempCtor = function() {};
  69. return function(ctor, args){
  70. tempCtor.prototype = ctor.prototype;
  71. var instance = new tempCtor();
  72. ctor.prototype.constructor.apply(instance,args);
  73. return instance;
  74. }
  75. }();
  76.  
  77. var MyClass = function(arg1, arg2){
  78. };
  79.  
  80. //define a class-level create method
  81. MyClass.create = function(arg1, arg2){
  82. return new MyClass(arg1, arg2);
  83. };
  84.  
  85. //from with some other method call
  86. var instance = MyClass.create.apply(this, arguments); //'this' is irrelevant
  87.  
  88. function Something() {
  89. // Do nothing
  90. }
  91.  
  92. Something.prototype.init = function() {
  93. // Do init stuff
  94. };
  95.  
  96. function createSomething() {
  97. var s = new Something();
  98. s.init.apply(s, arguments);
  99. return s;
  100. }
  101.  
  102. var s = createSomething(a,b,c); // 's' is an instance of Something
  103.  
  104. function Items () {
  105. this.elems = [].slice.call(arguments);
  106. }
  107.  
  108. Items.prototype.sum = function () {
  109. return this.elems.reduce(function (sum, x) { return sum + x }, 0);
  110. };
  111.  
  112. var items = Object.create(Items.prototype);
  113. Items.apply(items, [ 1, 2, 3, 4 ]);
  114.  
  115. console.log(items.sum());
  116.  
  117. $ node t.js
  118. 10
  119.  
  120. function createSomething() {
  121. var q = [];
  122. for(var i = 0; i < arguments.length; i++)
  123. q.push("arguments[" + i + "]");
  124. return eval("new Something(" + q.join(",") + ")");
  125. }
  126.  
  127. var s;
  128. s = (function(func, args, ctor) {
  129. ctor.prototype = func.prototype;
  130. var child = new ctor, result = func.apply(child, args);
  131. return typeof result === "object" ? result : child;
  132. })(Something, [a, b, c], function() {});
  133.  
  134. SomeClass = function(arg1, arg2) {
  135. // ...
  136. }
  137.  
  138. ReflectUtil.newInstance('SomeClass', 5, 7);
  139.  
  140. /**
  141. * @param strClass:
  142. * class name
  143. * @param optionals:
  144. * constructor arguments
  145. */
  146. ReflectUtil.newInstance = function(strClass) {
  147. var args = Array.prototype.slice.call(arguments, 1);
  148. var clsClass = eval(strClass);
  149. function F() {
  150. return clsClass.apply(this, args);
  151. }
  152. F.prototype = clsClass.prototype;
  153. return new F();
  154. };
  155.  
  156. construct = (constructor, args) ->
  157. F = -> constructor.apply this, args
  158. F.prototype = constructor.prototype
  159. new F
  160.  
  161. createSomething = (->
  162. F = (args) -> Something.apply this, args
  163. F.prototype = Something.prototype
  164. return -> new Something arguments
  165. )()
  166.  
  167. function Something() {
  168. // deal with the "arguments" array
  169. }
  170. var obj = new Something.apply(null, [0, 0]); // doesn't work!
  171.  
  172. function Something(args) {
  173. // shorter, but will substitute a default if args.x is 0, false, "" etc.
  174. this.x = args.x || SOME_DEFAULT_VALUE;
  175.  
  176. // longer, but will only put in a default if args.x is not supplied
  177. this.x = (args.x !== undefined) ? args.x : SOME_DEFAULT_VALUE;
  178. }
  179. var obj = new Something({x: 0, y: 0});
  180.  
  181. function Something(args) {
  182. var x = args[0];
  183. var y = args[1];
  184. }
  185. var obj = new Something([0, 0]);
  186.  
  187. var Storage = undefined;
  188.  
  189. return ((Storage = (new Something(...))) == undefined? (undefined) : (Storage.apply(...)));
Add Comment
Please, Sign In to add comment