While this question have been asked before and many already answered it, my question is strictly about the prototype of the newly created functions.
If you read this piece of code, you will understand that it just works. Also here on codepen.
// main object
var Foo = {};
// main methods
Foo.render = {}; // the Render function to populate later
Foo.start = function(el,ops){
return new Actions(el,ops);
}
// secondary/utility functions
var Actions = function(el,ops){
this.el = document.querySelector(el);
this.ops = ops || {};
this.prepare(); // this builds the Foo.render functions
for (var p in this.ops){
Foo.render[p](this);
}
};
// Action methods
Actions.prototype.prepare = function(){
for (var p in this.ops) {
Foo.render[p] = function(that){ // or r[p]
that.el.style[p] = that.ops[p] + 'px';
}
}
}
// init
var action = new Foo.start('div',{left:15})
// check
console.log(Foo.render['left'].prototype);
<div></div>
The problem is the prototype of the newly created function Foo.render['left']
is something like this Foo.render.(anonymous function) {}
instead of something like Foo.render.left() {}
or something else, and I am experiencing some performance loss because I am unable to access the newly created function's prototype very fast.
Can anyone please shed some light on how to adapt the .prepare()
function to create accurate/accessible (I can't chose the right word) prototype functions within the Foo
scope?
Thank you.