Is it possible to give partial javascript behavior like C # or fixing monkeys like Ruby does?

The idea of ​​partial classes is that you can group specific functions together. The best example of this in C # is to define controls in one file and event handlers in another. In Ruby, you can use Monkey patching to replace entire functions, etc., to get the code to do what you want.

I have not yet found a reason to do this, but I believe that as the network improves, more applications will be on the client side, so I'm wondering if there are some of the great features, side languages, I can also use in Javascript.

Somebody knows?

+4
source share
5
// file 1

function augment() {
    this.monkey = "monkey";
}

// file 2

function augmentMore() {
    this.patch = "patch";
}

// file 3

var o = {};
augment.call(o);
augmentMore.call(o);
console.log(o.monkey + o.patch);

. . , .

// file main
function SomeObject() {
    for (var i = 0, ii = SomeObject.Partial.length; i < ii; i++) {
         SomeObject.Partial[i].apply(this, arguments);
    }
}

SomeObject.Partial.SomeName = function() {
   ...
}

// file extra
SomeObject.Partial.SomeOtherName = function() {
   ...
}

JavaScript . , ?

+6

, :

# 1

function MyObjectType() { this.init(); }

# 2

MyObjectType.prototype.init = function() { this.one = 1; }

№ 3

MyObjectType.prototype.onClick = function() { if(this.one == 1) alert("I am so alone..."); }

:

var myObject = new MyObjectType();
myObject.onClick();

!

+3

Raynos. :

//  In Car.js
function Car(domelement, wheels, engine, color) {
    this.domelem = domelement;

    //  Wire in partial classes from other files
    for(var i = 0, ii = Car.Partial.length; i < ii; i++) {
         Car.Partial[i].apply(this, arguments);
    }
}
Car.Partial = [];  //  Prepare for declaration of additional partial classes

//  In Car.Events.js
Car.Partial[0] = function Events() {
    //  Create some events on Car with jQuery
    $(this.domelem).click(function() { alert('Car clicked.'); });
}

script, :

<script src="Car.js"></script>
<script src="Car.Events.js"></script>
<script>
    //  Turn first paragraph into our Car object
    var myCar = new Car($('p').get(0));
</script>
+3

ES6 ES5 Babel:

MyClass , :

index.js ( , )

symbols.js ( )

extraMethods.js (, )

index.js

import symbols from "./symbols";

export default class MyClass {
  [symbols.existentPrivateMethod]() {
    return "this is the result";
  }
}

import additionalMethod, {anotherAdditionalMethod, additionalPrivateMethod} from "./additionalMethods";
const additionalMethodsObject = {
  additionalMethod: additionalMethod,
  anotherAdditionalMethod: anotherAdditionalMethod
};
additionalMethodsObject[symbols.additionalPrivateMethod] = additionalPrivateMethod;

Object.assign(MyClass.prototype, additionalMethodsObject);

AdditionalMethods.js

import symbols from "./symbols";

export default function additionalMethod() {
  return this[symbols.existentPrivateMethod]();
}

export function anotherAdditionalMethod() {
  return this[symbols.additionalPrivateMethod]();
}

export function additionalPrivateMethod() {
  return "yet another result";
}

Symbol.js

const symbols = {
  existentPrivateMethod: Symbol("myPrivateMethod"),
  additionalPrivateMethod: Symbol("additionalPrivateMethod")
};

export default symbols;

https://github.com/nicosommi/partialClass

https://nicosommi.com/2015/08/10/partial-class-approach-for-es6/

+1

:

var Dog = Class.extend({
    init:function(data){
         data = data || {};
         this.name = data.name;
    },
    run:function(){ 
        /*impl*/
    }
});

/*other js file that require first file */
var Dog = Dog.extend({
   init:function(data){ 
       this._super(data); 
   },
   bark:function(){ 
       return 'woof';
   }
});

- , .

obs: using John Resing class.js , but can be written in typescript, ES6, AngularJs, ... and many other libraries.

0
source

All Articles