2

By API design I need something like this:

Store = function(fn, args) {
     return this[fn](args); 
}
Store.getOptions() { return this.options}
Store.setLog(args) { this.options.log = args}

Create objects instances from Store with separate options, Ex:

option1 = {save:false, log: true}
Store1 = new -->  Store(option1)  //pseudo code object creation

let opt1 = Store1('getOptions');   //Store acting like a function

Store1('setLogs', false);          //same call
Store1.setLogs(false);

Store act like a object(create new objects) and can be called.

I was trying with Store.bind(options) but defined properties getOptions and setLog get lost it.

3 Answers 3

1

Which EcmaScript version are you targeting: EcmaScript 5 or EcmaScript 6 (EcmaScript 2015)?

EcmaScript 5 class creation:

// Declare constructor for class Store:
var Store = function(){ /* This code will be executed when you call 'new Store()' */ }; 

NOTE: All properties and functions for instances of class are added to prototype property of class constructor function. */

// Add properties for each instance of class Store:
Store.prototype.options = {Hi:'World'}; // Each instance of Store will have object with property Hi:'World'

// Add functions for each instance of class Store:
Store.prototype.SayHello = function(name){
    console.log('Hello ', name);
};

Usage:

var store1 = new Store();
store1.SayHello('Phillip'); //Will output: Hello Phillip

EcmaScript 6 (EcmaScript 2015)

Take a look to article about classes usage in Mozilla Developer Network

Sign up to request clarification or add additional context in comments.

Comments

0

Why would you ever do?

let opt1 = Store1('getOptions');   //Store acting like a function
Store1('setLogs', false);          //same call

Just define the methods or compose them..

I can observe that you have 2 different use cases here:

  1. Getting/setting options
  2. Getting/setting log related stuff?

So why not:

var optionsProto = function() {
  var options = [];

  var getOption = function getOption(key) {
    if(options[key]) {
      return options[key];
    }

    //Handle error or return undefined..
  };

  var setOption = function setOption(key, val) {
    if(!options[key]) {
      options[key] = val;
    }

    //It already exists? Handle it somehow
  };

  return {
    getOption: getOption,
    setOption: setOption
  };
}();

var debuggerProto = function() {
  var level = 'DEBUG';

  var getLevel = function getLevel() {
    return level;
  };

  var setLevel = function setLevel(_level) {
    level = _level;
  };

  return {
    getLevel: getLevel,
    setLevel: setLevel
  };
}();


var mixedUpStoreProto = _.extend({}, optionsProto, debuggerProto);

var store = Object.create(mixedUpStoreProto);

store.setLevel('INFO');
store.getLevel(); //INFO

store.addOption('somekey', 'someval');
store.getOption('somekey'); // someval

This way you can compose a lot of objects by reusing the proto's.

Comments

0

First, thanks to @Epsil0neR and @gor181 for answers but is possible than I don´t do question correctly. Actually I ask again on Ramda Github issue because I need implement the API with curry functions. Finally, many hours late I go`it .

Here is the code, maybe is useful for somebody: https://github.com/ramda/ramda/issues/1291

//return 'fn' with all 'obj' props
function functionize(obj, fn)
{
    let fn = fn;
    for( let i in obj ){ fn[i] = obj[i] };
    return fn;
}


SubStore = function(options) {
    this.options = options;
}
SubStore.prototype.getOptions = function() { return this.options }
SubStore.prototype.setOptions = function(options) { this.options = options }
SubStore.prototype.insert = function(doc) { return doc }
SubStore.prototype.find = function(selector, options) { return selector }

SubStore.create = function(options) {
    let sub = functionize(new SubStore(options), R.curry(function (args, fn) {
        let self = sub;         //reference to new SubStore obj
        return self[fn](args);
    }))
    return sub;
}

Store = function(options) {
    this.options = options;
}
Store.prototype.insert = function(args) {
    for (let i = 0, len = args.length; i < len; i++) {
        if ( R.is(Function, args[i]) ) { args[i] = args[i]('insert') }
        console.log(args[i]);
    }
}
Store.prototype.find = function(args) {
    for (let i = 0, len = args.length; i < len; i++) {
        if ( R.is(Function, args[i]) ) { args[i] = args[i]('find') }
        console.log(args[i]);
    }
}

Test code

store = new Store();

sub1 = SubStore.create({path:'perfil'});
sub2 = SubStore.create({path:'rol'});

// test#1
store.insert([ sub1({name: "Foo"}), sub2({age: 18}) ]);

// test#2
store.insert([ sub1.insert({name: "Foo"}), sub2.insert({age: 18}) ]);

Is you need a Javascript ES5version just replace let with var and you must add Ramda library too for curry function.

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.