GithubHelp home page GithubHelp logo

proto's Introduction

Proto

This simple js library adds four functions to Object.prototype. Since this only modifies global objects and doesn't export any structures, you don't need the return value when calling require('proto').

Object.prototype

All functions added to Object.prototype are usable from any object in JavaScript.

Object.prototype.forEach(callback[, thisObject])

This is the most useful of the additions. It allows you to forEach over an Object instance's local properties and values just like you can already do with Array instances.

require('proto');
({name: "Tim", age: 28}).forEach(function (value, key) {
  console.log(key + " = " + JSON.stringify(value));
});

Object.prototype.map(callback[, thisObject])

This works like forEach, except returns an Array instance with the returned values of the function calls.

require('proto');
var pairs = ({name: "Tim", age: 28}).map(function (value, key) {
  return key + " = " + value;
});
// pairs is ["name = Tim", "age = 28"]

Object.prototype.new(args...)

Creates a new version of the current object and calls it's initialize function if one exists with the same arguments passed to new.

require('proto');
var Rectangle = {
  initialize: function initialize(width, height) {
    this.width = width;
    this.height = height;
  },
  get area() {
    return this.width * this.height;
  }
};

var rect = Rectangle.new(2, 4);
console.log(rect.area);

Object.prototype.extend(newObject)

Sets the current object as the prototype to the passed in object and returns the new passed in object.

// Assuming the code from above
var Square = Rectangle.extend({
  initialize: function initialize(side) {
    this.width = side;
    this.height = side;
  }
});
var square = Square.new(15);
console.log(square.area);

proto's People

Contributors

creationix avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

proto's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.