Guest User

Untitled

a guest
Feb 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.25 KB | None | 0 0
  1. <script type="text/javascript">
  2.  
  3. Function.prototype.partial = function(){
  4. var fn = this, args = Array.prototype.slice.call(arguments);
  5. return function(){
  6. var arg = 0;
  7. for ( var i = 0; i < args.length && arg < arguments.length; i++ ){
  8. if ( args[i] === undefined ){args[i] = arguments[arg++]; };
  9. };
  10. return fn.apply(this, args);
  11. };
  12. };
  13.  
  14. Function.prototype.curry = function() {
  15. var fn = this;
  16. var args = Array.prototype.slice.call(arguments);
  17. return function() {
  18. return fn.apply(this, args.concat(Array.prototype.slice.call(arguments)));
  19. };
  20. };
  21.  
  22. Function.prototype.bind = function (...args) {
  23. var fn = this;
  24. var oldArgs=args;
  25. return function (...args) {
  26. res=oldArgs.concat(args);
  27. return fn.apply(this,res);
  28. };
  29. };
  30.  
  31. /////////////////////////////
  32. /// Emulazione clasi
  33. ////////////////////////////
  34.  
  35.  
  36. function Class(prpProto, prpIndip, prpShrd, arr){
  37.  
  38. return new (function(){
  39. //variabili interne
  40. var slf=this;
  41. var cntIstances=0;
  42. var t=6;
  43. var proto={
  44. constructor: slf,
  45. espFun: function(){
  46. return t+=20;
  47. },
  48. };
  49. proto=Object.assign(proto, prpProto);
  50. var classConst={
  51. sheredProperties:(function(){
  52. var shrd={
  53. // Propietà condivise
  54. get shrPrp(){return cntIstances;},
  55. ///////////////////////
  56. espFun: function(){
  57. t=t+2;
  58. return t;
  59. },
  60. /////////////////
  61. };
  62. shrd.c='foo';
  63. shrd=Object.assign(shrd, prpShrd);
  64. shrd.__proto__=null;
  65. return shrd;
  66. })(),
  67. get prototype(){return proto;},
  68. };
  69. classConst.createIstance = (function indipendentPrp(){
  70. // Utilizziamo direttamente un oggetto in modo da ottenere
  71. //con esso il prototipo ad esso associato
  72. var c={
  73. // qui vanno le proprietà indipendenti dell'istanza
  74. prv:t,
  75. // format per accedere e restituire le variabili interne
  76. get t(){return t;},
  77. set t(x){},
  78. //////////////////////
  79. z_shrd: classConst.sheredProperties,
  80. get z_prototype(){return proto;},
  81. set z_prototype(x){Object.defineProperty(proto, x, {value: ''})},
  82. };
  83. prpIndip=Object.assign(c, prpIndip);
  84. cntIstances++;
  85. function init(args){
  86.  
  87. var _arr=args;
  88. return function _init(...args){
  89. if (args[0] instanceof Object){ //typeof(args[0])=='Object'
  90. args=args.pop()
  91. }
  92. var i=0;
  93. _arr.forEach(function(x){
  94. c[x]=args[i]||'';
  95. i++;
  96. });
  97. var res=Object.assign({}, c);
  98. res.__proto__=proto;
  99. return res;
  100. };
  101.  
  102. }
  103. return init(arr);
  104. })(),
  105.  
  106.  
  107.  
  108. // se si vuole avere alcune proprietà che non vengano ereditate dalle classi derivate
  109. // aggiungerle di seguito e decommentare il codice all'interno di createIstance
  110. //part.c="3";
  111. //return part;};
  112.  
  113. classConst.extend = function(parent, ...args){
  114. self=this;
  115. if (args.length){parent=parent.createIstance(args)};
  116. self.sheredProperties.__proto__=parent.z_shrd;
  117. proto.__proto__=parent;
  118. return function(){
  119. trg=self.createIstance(arguments);
  120.  
  121. return trg;
  122. }
  123. };
  124. return classConst;
  125. })(prpProto, prpIndip, prpShrd, arr);
  126. }
  127. var pss=Class({
  128. prtA: 'prt5',
  129. prtb: 'prt6'
  130. }, {
  131. indC: 'ind4',
  132. indD: 'ind3',
  133. }, {
  134. shrE:'shr5',
  135. shrF:'shr6'
  136. }, [
  137. 'pss_uno',
  138. 'pss_due'
  139. ]);
  140. var pss2=Class({
  141. prt2A: '2prt5',
  142. prtb2: '2prt6'
  143. }, {
  144. indC2: '2ind4',
  145. indD2: '2ind3',
  146. }, {
  147. shrE2:'2shr5',
  148. shrF2:'2shr6'
  149. }, [
  150. 'pss2_uno',
  151. 'pss2_due'
  152. ]);
  153.  
  154. //////////////// Test ///////////////////////////
  155.  
  156. console.log(pss.prototype.constructor);
  157.  
  158. var fre=pss.createIstance("freUno","freDue");
  159. //aaa.call(fre,4, 4);
  160. fre.x='12';
  161. console.log(fre.z_shrd);
  162. fre.espFun();
  163. fre.z_shrd.espFun();
  164. console.log(fre.z_shrd.shrPrp);
  165. console.log();
  166. debugger;
  167.  
  168. fre.z_prototype.q='1';
  169. var dde=pss.createIstance("ddeUno","ddeDue");
  170. dde.x='2';
  171. dde.z_prototype.q='4';
  172. dde.z_shrd.a='334';
  173. console.log(fre);
  174. console.log(dde);
  175. debugger;
  176. console.log('..................................');
  177. var flolk=pss.createIstance();
  178. console.log(flolk);
  179. var lol=pss.createIstance('',3);
  180. console.log(lol);
  181. var zz= pss2.extend(pss, "extProt", "extProt2")("istPss2", "istPss2_2");
  182. console.log();
  183. zz.espFun();
  184. zz.z_shrd.espFun();
  185. console.log(zz);
  186.  
  187.  
  188. //))))))))))))))))))))))))))))))))))))))))))))))))
  189.  
  190. /*var pss=new (function(){
  191. //variabili interne
  192. var slf=this;
  193. var cntIstances=0;
  194. var t=0;
  195. var proto={
  196. constructor: '',
  197. espFun: function(){
  198. return t+=20;
  199. }
  200. };
  201.  
  202. return metodiClasse={
  203. createIstance: //function(_a, _b){part=(
  204. function indipendentPrp(_a, _b){
  205. proto.constructor=indipendentPrp;
  206. // Utilizziamo direttamente un oggetto in modo da ottenere
  207. //con esso il prototipo ad esso associato
  208. c={
  209. // qui vanno le proprietà indipendenti dell'istanza
  210. a: _a,
  211. b: _b,
  212. clsProp: this.sheredProperties,
  213. // format per accedere e restituire le variabili interne
  214. get t(){return t;},
  215. set t(x){},
  216. get prototype(){return proto;},
  217. set prototype(x){Object.defineProperty(proto, x, {value: ''})},
  218. __proto__: proto,
  219. };
  220. cntIstances++;
  221. return c;
  222. },//)(_a, _b);
  223. // se si vuole avere alcune proprietà che non vengano ereditate dalle classi derivate
  224. // aggiungerle di seguito e decommentare il codice all'interno di createIstance
  225. //part.c="3";
  226. //return part;};
  227.  
  228. extend: function(prty){
  229. proto.__proto__=prty;
  230. self=this;
  231. return function(){
  232. trg=self.createIstance(arguments[0],arguments[1]);
  233. trg1=prty.constructor(arguments[2],arguments[3]);
  234. trg=Object.assign(trg1, trg);
  235. return trg;
  236. }
  237. },
  238. sheredProperties:(function(){
  239. shrd={
  240. espFun: function(){
  241. t=t+2;
  242. return t;
  243. },
  244. get shrPrp(){return cntIstances;},
  245. };
  246. shrd.c='foo';
  247. return shrd;
  248. })(),
  249. get prototype(){return proto;},
  250. }
  251. })();
  252.  
  253.  
  254.  
  255. //////////////////// test //////////////////////////////
  256.  
  257. // format per il mixing
  258. function aaa(){
  259. this.pstA='a';
  260. this.pstB=arguments[0];
  261. this.pstC=arguments[1];
  262. };
  263. function aaa2(){
  264. this.pstA2='a';
  265. this.pstB2=arguments[0];
  266. this.pstC2=arguments[1];
  267. };
  268. aaa.call(pss, 0, 1);
  269. aaa2.call(pss2, 3,4);
  270.  
  271. // il seguente format si avvale dell'oggetto restituito da get prototype
  272.  
  273. pss.prototype.ptUno='uno';
  274. pss.prototype.ptDue='due';
  275. pss.prototype.ptUno2='uno';
  276. pss.prototype.ptDue2='due';*/
  277.  
  278.  
  279. var fre=pss.createIstance(1,2);
  280. //aaa.call(fre,4, 4);
  281. fre.x='12';
  282. console.log(fre.clsProp);
  283. fre.espFun();
  284. fre.clsProp.espFun();
  285. console.log(fre.clsProp.shrPrp);
  286. console.log();
  287. debugger;
  288.  
  289. fre.prototype.q='1';
  290. var dde=pss.createIstance(4,5);
  291. dde.x='2';
  292. dde.prototype.q='4';
  293. dde.clsProp.a='334';
  294. console.log(fre);
  295. console.log(dde);
  296. debugger;
  297. var zz= pss2.extend(pss.prototype)(33, 22, 44, 11);
  298. console.log();
  299. zz.espFun();
  300. zz.clsProp.espFun();
  301. console.log(zz);
  302.  
  303. </script>
  304. </body>
  305. </html>
  306.  
  307.  
  308. <script>
  309.  
  310. //////////////////////////////////////
  311. /// Emulare new
  312. ////////////////////////////////
  313.  
  314. function Test(x,y){
  315. this.istA='a';
  316. this.istB=;
  317. this.istC=y;
  318. };
  319. Test.prototype.proB='ciao';
  320.  
  321. var baz= new Test();
  322.  
  323. var foo={};
  324.  
  325. //aggiungiamo il ptototipo
  326. foo.__proto__ = Test.prototype;
  327.  
  328. //aggiungo proprietà all'istanza
  329. Test.call(foo);
  330.  
  331. //verifiche
  332. console.log(Test.prototype.constructor===foo.constructor);
  333. console.log(foo instanceof Test);
  334. console.log(foo.proB);
  335. console.log(foo.istA);
  336.  
  337. </script>
  338.  
  339.  
  340.  
  341. //////////////////////////////////////
  342. //// Parzial e Curry
  343. ///////////////////////////
  344.  
  345.  
  346. <script type="text/javascript">
  347.  
  348. Function.prototype.partial = function(){
  349. var fn = this, args = Array.prototype.slice.call(arguments);
  350. return function(){
  351. var arg = 0;
  352. for ( var i = 0; i < args.length && arg < arguments.length; i++ ){
  353. if ( args[i] === undefined ){args[i] = arguments[arg++]; };
  354. };
  355. return fn.apply(this, args);
  356. };
  357. };
  358.  
  359. Function.prototype.curry = function() {
  360. var fn = this;
  361. var args = Array.prototype.slice.call(arguments);
  362. return function() {
  363. return fn.apply(this, args.concat(Array.prototype.slice.call(arguments)));
  364. };
  365. };
  366.  
  367. /////////////////////////////
  368. /// Emulazione clasi
  369. ////////////////////////////
  370.  
  371. <!DOCTYPE html>
  372. <html>
  373. <head>
  374. <title>Page Title</title>
  375. </head>
  376. <body>
  377. <script>
  378. debugger;
  379. function Class(){
  380.  
  381. new (function(){
  382. //variabili interne
  383. var slf=this;
  384. var cntIstances=0;
  385. var t=0;
  386. var proto={
  387. constructor: '',
  388. espFun: function(){
  389. return t+=20;
  390. }
  391. Object.assign(proto, prpProto);
  392. };
  393. return metodiClasse={
  394. createIstance: //function(_a, _b){part=(
  395. function indipendentPrp(_a, _b){
  396. proto.constructor=indipendentPrp;
  397. // Utilizziamo direttamente un oggetto in modo da ottenere
  398. //con esso il prototipo ad esso associato
  399. c={
  400. // qui vanno le proprietà indipendenti dell'istanza
  401. a: _a,
  402. b: _b,
  403. // format per accedere e restituire le variabili interne
  404. get t(){return t;},
  405. set t(x){},
  406. //////////////////////
  407. clsProp: this.sheredProperties,
  408. get prototype(){return proto;},
  409. set prototype(x){Object.defineProperty(proto, x, {value: ''})},
  410. __proto__: proto,
  411. };
  412. Object.assign(proto, prpIndip);
  413. cntIstances++;
  414. return c;
  415. },//)(_a, _b);
  416. // se si vuole avere alcune proprietà che non vengano ereditate dalle classi derivate
  417. // aggiungerle di seguito e decommentare il codice all'interno di createIstance
  418. //part.c="3";
  419. //return part;};
  420.  
  421. extend: function(prty){
  422. proto.__proto__=prty;
  423. self=this;
  424. return function(){
  425. trg=self.createIstance(arguments[0],arguments[1]);
  426. trg1=prty.constructor(arguments[2],arguments[3]);
  427. trg=Object.assign(trg1, trg);
  428. return trg;
  429. }
  430. },
  431. sheredProperties:(function(){
  432. shrd={
  433. // Propietà condivise
  434. get shrPrp(){return cntIstances;},
  435. ///////////////////////
  436. espFun: function(){
  437. t=t+2;
  438. return t;
  439. },
  440. /////////////////
  441. }c='foo';
  442. Object.assign(proto, prpShrd);
  443. return shrd;
  444. })(),
  445. get prototype(){return proto;},
  446. }
  447. })(prpProto, prpIndip, prpShrd);
  448.  
  449. var pss=new (function(){
  450. //variabili interne
  451. var slf=this;
  452. var cntIstances=0;
  453. var t=0;
  454. var proto={
  455. constructor: '',
  456. espFun: function(){
  457. return t+=20;
  458. }
  459. };
  460.  
  461. return metodiClasse={
  462. createIstance: //function(_a, _b){part=(
  463. function indipendentPrp(_a, _b){
  464. proto.constructor=indipendentPrp;
  465. // Utilizziamo direttamente un oggetto in modo da ottenere
  466. //con esso il prototipo ad esso associato
  467. c={
  468. // qui vanno le proprietà indipendenti dell'istanza
  469. a: _a,
  470. b: _b,
  471. clsProp: this.sheredProperties,
  472. // format per accedere e restituire le variabili interne
  473. get t(){return t;},
  474. set t(x){},
  475. get prototype(){return proto;},
  476. set prototype(x){Object.defineProperty(proto, x, {value: ''})},
  477. __proto__: proto,
  478. };
  479. cntIstances++;
  480. return c;
  481. },//)(_a, _b);
  482. // se si vuole avere alcune proprietà che non vengano ereditate dalle classi derivate
  483. // aggiungerle di seguito e decommentare il codice all'interno di createIstance
  484. //part.c="3";
  485. //return part;};
  486.  
  487. extend: function(prty){
  488. proto.__proto__=prty;
  489. self=this;
  490. return function(){
  491. trg=self.createIstance(arguments[0],arguments[1]);
  492. trg1=prty.constructor(arguments[2],arguments[3]);
  493. trg=Object.assign(trg1, trg);
  494. return trg;
  495. }
  496. },
  497. sheredProperties:(function(){
  498. shrd={
  499. espFun: function(){
  500. t=t+2;
  501. return t;
  502. },
  503. get shrPrp(){return cntIstances;},
  504. };
  505. shrd.c='foo';
  506. return shrd;
  507. })(),
  508. get prototype(){return proto;},
  509. }
  510. })();
  511.  
  512.  
  513.  
  514. //////////////////// test //////////////////////////////
  515.  
  516. // format per il mixing
  517. function aaa(){
  518. this.pstA='a';
  519. this.pstB=arguments[0];
  520. this.pstC=arguments[1];
  521. };
  522. function aaa2(){
  523. this.pstA2='a';
  524. this.pstB2=arguments[0];
  525. this.pstC2=arguments[1];
  526. };
  527. aaa.call(pss, 0, 1);
  528. aaa2.call(pss2, 3,4);
  529.  
  530. // il seguente format si avvale dell'oggetto restituito da get prototype
  531.  
  532. pss.prototype.ptUno='uno';
  533. pss.prototype.ptDue='due';
  534. pss.prototype.ptUno2='uno';
  535. pss.prototype.ptDue2='due';
  536.  
  537.  
  538. var fre=pss.createIstance(1,2);
  539. aaa.call(fre,4, 4);
  540. fre.x='12';
  541. console.log(fre.clsProp);
  542. fre.espFun();
  543. fre.clsProp.espFun();
  544. console.log(fre.clsProp.shrPrp);
  545. console.log();
  546. debugger;
  547.  
  548. fre.prototype.q='1';
  549. var dde=pss.createIstance(4,5);
  550. dde.x='2';
  551. dde.prototype.q='4';
  552. dde.clsProp.a='334';
  553. console.log(fre);
  554. console.log(dde);
  555. debugger;
  556. var zz= pss2.extend(pss.prototype)(33, 22, 44, 11);
  557. console.log();
  558. zz.espFun();
  559. zz.clsProp.espFun2();
  560. console.log(zz);
  561. </script>
  562.  
  563. </body>
  564. </html>
  565. </html>
Add Comment
Please, Sign In to add comment