Disclaimer: (1) My back platform is in Java / C #, and I just started my deep dive in JavaScript last week. (2) I know the _.mixin () method.
This is not a critical project, but rather, I am having problems with overlaying object inheritance in Javascript.
For example, I tried to increase underscore.js using the _.keyFilter function (I understand that I can use the map to achieve something similar) to return a list of keys that satisfy the evaluation function. I can achieve the result using the _.mixin () method:
Besides dropping the function directly into the source code, _.mixin ()
_.mixin({ filterKey : function(obj, iterator, context) {
var results = [];
if (obj == null) return results;
_.each(obj, function(value, index, list) {
if (iterator.call(context, value, index, list)){
results[results.length] = index;
}
});
return results;
}});
However, I'm not sure I understand why I cannot just increase the underline with the following in the script file:
_.keyFilter = function(obj, iterator, context) {
var results = [];
if (obj == null) return results;
each(obj, function(value, index, list) {
if (iterator.call(context, value, index, list)) results[results.length] = index;
});
return results;
};
, :
_.chain(myList).keyFilter(evalFunction);
:
'Uncaught TypeError: Object [object Object] has no method 'keyFilter'
, , underscore.js.
, ( ) script:
var keyFilter = _.prototype.keyFilter = function(obj, iterator, context) {
var results = [];
if (obj == null) return results;
each(obj, function(value, index, list) {
if (iterator.call(context, value, index, list)) results[results.length] = index;
});
return results;
};
. ( , var script, ).
.