Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script type="text/javascript">
- Function.prototype.partial = function(){
- var fn = this, args = Array.prototype.slice.call(arguments);
- return function(){
- var arg = 0;
- for ( var i = 0; i < args.length && arg < arguments.length; i++ ){
- if ( args[i] === undefined ){args[i] = arguments[arg++]; };
- };
- return fn.apply(this, args);
- };
- };
- Function.prototype.curry = function() {
- var fn = this;
- var args = Array.prototype.slice.call(arguments);
- return function() {
- return fn.apply(this, args.concat(Array.prototype.slice.call(arguments)));
- };
- };
- Function.prototype.bind = function (...args) {
- var fn = this;
- var oldArgs=args;
- return function (...args) {
- res=oldArgs.concat(args);
- return fn.apply(this,res);
- };
- };
- /////////////////////////////
- /// Emulazione clasi
- ////////////////////////////
- function Class(prpProto, prpIndip, prpShrd, arr){
- return new (function(){
- //variabili interne
- var slf=this;
- var cntIstances=0;
- var t=6;
- var proto={
- constructor: slf,
- espFun: function(){
- return t+=20;
- },
- };
- proto=Object.assign(proto, prpProto);
- var classConst={
- sheredProperties:(function(){
- var shrd={
- // Propietà condivise
- get shrPrp(){return cntIstances;},
- ///////////////////////
- espFun: function(){
- t=t+2;
- return t;
- },
- /////////////////
- };
- shrd.c='foo';
- shrd=Object.assign(shrd, prpShrd);
- shrd.__proto__=null;
- return shrd;
- })(),
- get prototype(){return proto;},
- };
- classConst.createIstance = (function indipendentPrp(){
- // Utilizziamo direttamente un oggetto in modo da ottenere
- //con esso il prototipo ad esso associato
- var c={
- // qui vanno le proprietà indipendenti dell'istanza
- prv:t,
- // format per accedere e restituire le variabili interne
- get t(){return t;},
- set t(x){},
- //////////////////////
- z_shrd: classConst.sheredProperties,
- get z_prototype(){return proto;},
- set z_prototype(x){Object.defineProperty(proto, x, {value: ''})},
- };
- prpIndip=Object.assign(c, prpIndip);
- cntIstances++;
- function init(args){
- var _arr=args;
- return function _init(...args){
- if (args[0] instanceof Object){ //typeof(args[0])=='Object'
- args=args.pop()
- }
- var i=0;
- _arr.forEach(function(x){
- c[x]=args[i]||'';
- i++;
- });
- var res=Object.assign({}, c);
- res.__proto__=proto;
- return res;
- };
- }
- return init(arr);
- })(),
- // se si vuole avere alcune proprietà che non vengano ereditate dalle classi derivate
- // aggiungerle di seguito e decommentare il codice all'interno di createIstance
- //part.c="3";
- //return part;};
- classConst.extend = function(parent, ...args){
- self=this;
- if (args.length){parent=parent.createIstance(args)};
- self.sheredProperties.__proto__=parent.z_shrd;
- proto.__proto__=parent;
- return function(){
- trg=self.createIstance(arguments);
- return trg;
- }
- };
- return classConst;
- })(prpProto, prpIndip, prpShrd, arr);
- }
- var pss=Class({
- prtA: 'prt5',
- prtb: 'prt6'
- }, {
- indC: 'ind4',
- indD: 'ind3',
- }, {
- shrE:'shr5',
- shrF:'shr6'
- }, [
- 'pss_uno',
- 'pss_due'
- ]);
- var pss2=Class({
- prt2A: '2prt5',
- prtb2: '2prt6'
- }, {
- indC2: '2ind4',
- indD2: '2ind3',
- }, {
- shrE2:'2shr5',
- shrF2:'2shr6'
- }, [
- 'pss2_uno',
- 'pss2_due'
- ]);
- //////////////// Test ///////////////////////////
- console.log(pss.prototype.constructor);
- var fre=pss.createIstance("freUno","freDue");
- //aaa.call(fre,4, 4);
- fre.x='12';
- console.log(fre.z_shrd);
- fre.espFun();
- fre.z_shrd.espFun();
- console.log(fre.z_shrd.shrPrp);
- console.log();
- debugger;
- fre.z_prototype.q='1';
- var dde=pss.createIstance("ddeUno","ddeDue");
- dde.x='2';
- dde.z_prototype.q='4';
- dde.z_shrd.a='334';
- console.log(fre);
- console.log(dde);
- debugger;
- console.log('..................................');
- var flolk=pss.createIstance();
- console.log(flolk);
- var lol=pss.createIstance('',3);
- console.log(lol);
- var zz= pss2.extend(pss, "extProt", "extProt2")("istPss2", "istPss2_2");
- console.log();
- zz.espFun();
- zz.z_shrd.espFun();
- console.log(zz);
- //))))))))))))))))))))))))))))))))))))))))))))))))
- /*var pss=new (function(){
- //variabili interne
- var slf=this;
- var cntIstances=0;
- var t=0;
- var proto={
- constructor: '',
- espFun: function(){
- return t+=20;
- }
- };
- return metodiClasse={
- createIstance: //function(_a, _b){part=(
- function indipendentPrp(_a, _b){
- proto.constructor=indipendentPrp;
- // Utilizziamo direttamente un oggetto in modo da ottenere
- //con esso il prototipo ad esso associato
- c={
- // qui vanno le proprietà indipendenti dell'istanza
- a: _a,
- b: _b,
- clsProp: this.sheredProperties,
- // format per accedere e restituire le variabili interne
- get t(){return t;},
- set t(x){},
- get prototype(){return proto;},
- set prototype(x){Object.defineProperty(proto, x, {value: ''})},
- __proto__: proto,
- };
- cntIstances++;
- return c;
- },//)(_a, _b);
- // se si vuole avere alcune proprietà che non vengano ereditate dalle classi derivate
- // aggiungerle di seguito e decommentare il codice all'interno di createIstance
- //part.c="3";
- //return part;};
- extend: function(prty){
- proto.__proto__=prty;
- self=this;
- return function(){
- trg=self.createIstance(arguments[0],arguments[1]);
- trg1=prty.constructor(arguments[2],arguments[3]);
- trg=Object.assign(trg1, trg);
- return trg;
- }
- },
- sheredProperties:(function(){
- shrd={
- espFun: function(){
- t=t+2;
- return t;
- },
- get shrPrp(){return cntIstances;},
- };
- shrd.c='foo';
- return shrd;
- })(),
- get prototype(){return proto;},
- }
- })();
- //////////////////// test //////////////////////////////
- // format per il mixing
- function aaa(){
- this.pstA='a';
- this.pstB=arguments[0];
- this.pstC=arguments[1];
- };
- function aaa2(){
- this.pstA2='a';
- this.pstB2=arguments[0];
- this.pstC2=arguments[1];
- };
- aaa.call(pss, 0, 1);
- aaa2.call(pss2, 3,4);
- // il seguente format si avvale dell'oggetto restituito da get prototype
- pss.prototype.ptUno='uno';
- pss.prototype.ptDue='due';
- pss.prototype.ptUno2='uno';
- pss.prototype.ptDue2='due';*/
- var fre=pss.createIstance(1,2);
- //aaa.call(fre,4, 4);
- fre.x='12';
- console.log(fre.clsProp);
- fre.espFun();
- fre.clsProp.espFun();
- console.log(fre.clsProp.shrPrp);
- console.log();
- debugger;
- fre.prototype.q='1';
- var dde=pss.createIstance(4,5);
- dde.x='2';
- dde.prototype.q='4';
- dde.clsProp.a='334';
- console.log(fre);
- console.log(dde);
- debugger;
- var zz= pss2.extend(pss.prototype)(33, 22, 44, 11);
- console.log();
- zz.espFun();
- zz.clsProp.espFun();
- console.log(zz);
- </script>
- </body>
- </html>
- <script>
- //////////////////////////////////////
- /// Emulare new
- ////////////////////////////////
- function Test(x,y){
- this.istA='a';
- this.istB=;
- this.istC=y;
- };
- Test.prototype.proB='ciao';
- var baz= new Test();
- var foo={};
- //aggiungiamo il ptototipo
- foo.__proto__ = Test.prototype;
- //aggiungo proprietà all'istanza
- Test.call(foo);
- //verifiche
- console.log(Test.prototype.constructor===foo.constructor);
- console.log(foo instanceof Test);
- console.log(foo.proB);
- console.log(foo.istA);
- </script>
- //////////////////////////////////////
- //// Parzial e Curry
- ///////////////////////////
- <script type="text/javascript">
- Function.prototype.partial = function(){
- var fn = this, args = Array.prototype.slice.call(arguments);
- return function(){
- var arg = 0;
- for ( var i = 0; i < args.length && arg < arguments.length; i++ ){
- if ( args[i] === undefined ){args[i] = arguments[arg++]; };
- };
- return fn.apply(this, args);
- };
- };
- Function.prototype.curry = function() {
- var fn = this;
- var args = Array.prototype.slice.call(arguments);
- return function() {
- return fn.apply(this, args.concat(Array.prototype.slice.call(arguments)));
- };
- };
- /////////////////////////////
- /// Emulazione clasi
- ////////////////////////////
- <!DOCTYPE html>
- <html>
- <head>
- <title>Page Title</title>
- </head>
- <body>
- <script>
- debugger;
- function Class(){
- new (function(){
- //variabili interne
- var slf=this;
- var cntIstances=0;
- var t=0;
- var proto={
- constructor: '',
- espFun: function(){
- return t+=20;
- }
- Object.assign(proto, prpProto);
- };
- return metodiClasse={
- createIstance: //function(_a, _b){part=(
- function indipendentPrp(_a, _b){
- proto.constructor=indipendentPrp;
- // Utilizziamo direttamente un oggetto in modo da ottenere
- //con esso il prototipo ad esso associato
- c={
- // qui vanno le proprietà indipendenti dell'istanza
- a: _a,
- b: _b,
- // format per accedere e restituire le variabili interne
- get t(){return t;},
- set t(x){},
- //////////////////////
- clsProp: this.sheredProperties,
- get prototype(){return proto;},
- set prototype(x){Object.defineProperty(proto, x, {value: ''})},
- __proto__: proto,
- };
- Object.assign(proto, prpIndip);
- cntIstances++;
- return c;
- },//)(_a, _b);
- // se si vuole avere alcune proprietà che non vengano ereditate dalle classi derivate
- // aggiungerle di seguito e decommentare il codice all'interno di createIstance
- //part.c="3";
- //return part;};
- extend: function(prty){
- proto.__proto__=prty;
- self=this;
- return function(){
- trg=self.createIstance(arguments[0],arguments[1]);
- trg1=prty.constructor(arguments[2],arguments[3]);
- trg=Object.assign(trg1, trg);
- return trg;
- }
- },
- sheredProperties:(function(){
- shrd={
- // Propietà condivise
- get shrPrp(){return cntIstances;},
- ///////////////////////
- espFun: function(){
- t=t+2;
- return t;
- },
- /////////////////
- }c='foo';
- Object.assign(proto, prpShrd);
- return shrd;
- })(),
- get prototype(){return proto;},
- }
- })(prpProto, prpIndip, prpShrd);
- var pss=new (function(){
- //variabili interne
- var slf=this;
- var cntIstances=0;
- var t=0;
- var proto={
- constructor: '',
- espFun: function(){
- return t+=20;
- }
- };
- return metodiClasse={
- createIstance: //function(_a, _b){part=(
- function indipendentPrp(_a, _b){
- proto.constructor=indipendentPrp;
- // Utilizziamo direttamente un oggetto in modo da ottenere
- //con esso il prototipo ad esso associato
- c={
- // qui vanno le proprietà indipendenti dell'istanza
- a: _a,
- b: _b,
- clsProp: this.sheredProperties,
- // format per accedere e restituire le variabili interne
- get t(){return t;},
- set t(x){},
- get prototype(){return proto;},
- set prototype(x){Object.defineProperty(proto, x, {value: ''})},
- __proto__: proto,
- };
- cntIstances++;
- return c;
- },//)(_a, _b);
- // se si vuole avere alcune proprietà che non vengano ereditate dalle classi derivate
- // aggiungerle di seguito e decommentare il codice all'interno di createIstance
- //part.c="3";
- //return part;};
- extend: function(prty){
- proto.__proto__=prty;
- self=this;
- return function(){
- trg=self.createIstance(arguments[0],arguments[1]);
- trg1=prty.constructor(arguments[2],arguments[3]);
- trg=Object.assign(trg1, trg);
- return trg;
- }
- },
- sheredProperties:(function(){
- shrd={
- espFun: function(){
- t=t+2;
- return t;
- },
- get shrPrp(){return cntIstances;},
- };
- shrd.c='foo';
- return shrd;
- })(),
- get prototype(){return proto;},
- }
- })();
- //////////////////// test //////////////////////////////
- // format per il mixing
- function aaa(){
- this.pstA='a';
- this.pstB=arguments[0];
- this.pstC=arguments[1];
- };
- function aaa2(){
- this.pstA2='a';
- this.pstB2=arguments[0];
- this.pstC2=arguments[1];
- };
- aaa.call(pss, 0, 1);
- aaa2.call(pss2, 3,4);
- // il seguente format si avvale dell'oggetto restituito da get prototype
- pss.prototype.ptUno='uno';
- pss.prototype.ptDue='due';
- pss.prototype.ptUno2='uno';
- pss.prototype.ptDue2='due';
- var fre=pss.createIstance(1,2);
- aaa.call(fre,4, 4);
- fre.x='12';
- console.log(fre.clsProp);
- fre.espFun();
- fre.clsProp.espFun();
- console.log(fre.clsProp.shrPrp);
- console.log();
- debugger;
- fre.prototype.q='1';
- var dde=pss.createIstance(4,5);
- dde.x='2';
- dde.prototype.q='4';
- dde.clsProp.a='334';
- console.log(fre);
- console.log(dde);
- debugger;
- var zz= pss2.extend(pss.prototype)(33, 22, 44, 11);
- console.log();
- zz.espFun();
- zz.clsProp.espFun2();
- console.log(zz);
- </script>
- </body>
- </html>
- </html>
Add Comment
Please, Sign In to add comment