Nodejs Object Bind map(f)

Here you can find the source of map(f)

Method Source Code

"use strict";//from ww w .  ja  v  a2 s.co  m

var map = function(f, thing) {
  if(thing.map) {
    return thing.map(f);
  }

  throw new Error("Object given does not implement map");
};

var square = function(x) {
  return x * x;
};

console.log(map(square, [1,2,3]));

Object.prototype.map = function(f) {
  var result = {}, that = this;

  Object.keys(this).forEach(function(key) {
    result[key] = f(key, that[key]);
  });

  return result;
};

var obj = {
  "courses" : ["Programming 101", "Frontend JavaScript", "Core Java"]
};

console.log(map(function(key, value) {
  return map(function(course) {
    return course + "!";
  }, value);
}, obj));


String.prototype.map = function(f) {
  var result = this.split("");
  return map(f, result).join("");
};

console.log(map(function(ch) {
  return ch.toUpperCase();
}, "javascript"));

Related

  1. bind(method)
    Object.prototype.bind = function(method){
      var object  = this;
      return function(){
            method.apply(object, arguments);
        };
    
  2. bind(prop)
    Object.prototype.bind = function (prop) {
        var self = this;
        var method = this[prop];
        return function () {
      return method.apply(self, arguments);
        };
    };
    
  3. map(cb, thisArg)
    Object.prototype.map = function(cb, thisArg) {
      thisArg = thisArg || this;
      var that = this;
      Object.getOwnPropertyNames(this).map(function(name) {
        cb(name, that[name]);
      });
      return this;
    };
    
  4. map(obj, callback)
    "use strict";
    Object.map = function(obj, callback) {
        return Object.keys(obj).map(key => callback(obj[key], key));
    
  5. mapWithKey(f)
    Object.prototype.mapWithKey = function(f) {
      var result = new Array();
      for (var key in this) {
        result[result.length] = f(key, this[key]);
      };
      return result;
    };
    
  6. addMixin(mixin)
    Object.prototype.addMixin = function (mixin) {    
      for (var prop in mixin) {
        if (mixin.hasOwnProperty(prop)) {
          this.prototype[prop] = mixin[prop];
    };