GithubHelp home page GithubHelp logo

base.js's Introduction

From http://dean.edwards.name/weblog/2006/03/base/

The Base Class

I’ve created a JavaScript class (Base.js) that I hope eases the pain of JavaScript OO. It’s a simple class and extends the Object object by adding two instance methods and one class method.

The Base class defines two instance methods:

extend

Call this method to extend an object with another interface:

var object = new Base;
object.extend({
  value: "some data",
  method: function() {
    alert("Hello World!");
  }
});
object.method();
// ==> Hello World!

base

If a method has been overridden then the base method provides access to the overridden method:

var object = new Base;
object.method = function() {
  alert("Hello World!");
};
object.extend({
  method: function() {
    // call the "super" method
    this.base();
    // add some code
    alert("Hello again!");
  }
});
object.method();
// ==> Hello World!
// ==> Hello again!

You can also call the base method from within a constructor function.

Creating Classes

Classes are created by calling the extend method on the Base class:

var Animal = Base.extend({
  constructor: function(name) {
    this.name = name;
  },

  name: "",

  eat: function() {
    this.say("Yum!");
  },

  say: function(message) {
    alert(this.name + ": " + message);
  }
});

All classes created in this manner will inherit the extend method so we can easily subclass the Animal class:

var Cat = Animal.extend({
  eat: function(food) {
    if (food instanceof Mouse) this.base();
    else this.say("Yuk! I only eat mice.");
  }
});

var Mouse = Animal.extend();

Class Properties and Methods

A second parameter passed to the extend method of a class defines the class interface:

var Circle = Shape.extend({ // instance interface
  constructor: function(x, y, radius) {
    this.base(x, y);
    this.radius = radius;
  },

  radius: 0,

  getCircumference: function() {
    return 2 * Circle.PI * this.radius;
  }
}, { // class interface
  PI: 3.14
});

Note the use of the base method in the constructor. This ensures that the Shape constructor is also called. Some other things to note:

  • If you define a class method (not an instance method) called init it will be automatically called when the class is created
  • Constructor functions are never called during the prototyping phase (subclassing)

Classes With Private Data

Some developers prefer to create classes where methods access private data:

function Circle(radius) {
  this.getCircumference = function() {
    return 2 * Math.PI * radius;
  };
};

You can achieve the same result using the Base class:

var Circle = Shape.extend({
  constructor: function(radius) {
    this.extend({
      getCircumference: function() {
        return 2 * Math.PI * radius;
      }
    });
  }
});

The code is slightly more verbose in this case but you get access to the base method which I find incredibly useful.

Single Instances

I changed my mind a lot about this but finally decided to allow the creation of single instance classes by defining a null constructor:

var Math = Base.extend({
  constructor: null,
  PI: 3.14,
  sqr: function(number) {
    return number * number;
  }
});

base.js's People

Contributors

jasonmadigan 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.