Giter VIP home page Giter VIP logo

proto.js's Introduction

Proto.js - Prototypical Inheritance Embraced

Prototypical inheritence in JavaScript is actually kind of cool, but in addition to deviating from the inheritance most people are used to, it's annoyingly hard to use (Object.create wasn't even added until ES5, and even that is a little unfriendly). Proto.js embraces JavaScripts prototypical inheritance and makes it easy to create new objects that extend a prototype using a concise syntax enabled by JavaScript's object literals.

.create()

create is how you create new objects that extend other objects:

// indicate that an object is a prototype with StudlyCaps
var Person = Proto.create({
	greet: function() {
		return "Hello " + this.name + "!".
	}
});

var joe = Person.create({name: "Joe"});
joe.greet() --> "Hello Joe!"

.extend()

Proto.js objects are re-openable, and extend allows you to attach methods and objects to existing objects just like during create:

Person.extend({
	greetLoudly: function() {
		return this.greet().toUpperCase();
	}
});

joe.greetLoudly() --> "HELLO JOE!";

._super()

Proto.js object make it possible to override existing methods, but still call them:

var LoudPerson = Person.create({
	greet: function() {
		// Lets us search the prototype chain starting above the object we pass in.
		var message = this._super(LoudPerson).greet();
		return message.toUpperCase();
	}
});

var jill = LoudPerson.create({name: "Jill"});
jill.greet() --> "HELLO JILL!";

Never write $.proxy again!

Methods on Proto.js objects are bound by default (you can still get at the unbound methods in __methods__ if you need to):

var f = joe.greet;
f() --> "Hello Joe!"

var g = joe.__methods__.greet;
g() --> "Hello undefined!"

License

Licensed under the Apache Public License v2.0

proto.js's People

Contributors

zacherates avatar

Watchers

 avatar

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.