Inheritance.js 0.3.2 Sample: Create New Object Definition

by Brandon

HTML

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/normalize/3.0.3/normalize.min.css">
<script src="https://dl.dropboxusercontent.com/u/262650966/inheritance.js/0.3.2/inheritance.min.js"></script>
<h1>Inheritance.js 0.3.2 Sample: Create New Object Definition</h1>
<p>Refer to the "JavaScript" panel for sample code.</p>
<p>Open your dev console to view sample output.</p>

CSS

body {
  padding: 50px;
}

JavaScript

// =============================================== //
// Inheritance.js 0.3.2 Sample:                    //
//   Create a New Object Definition (I.E. "Class") //
// =============================================== //



// Mixins are assumed to be simple objects and you are highly encouraged
// to keep them that way given what a mixin is. However, mixins are not
// required to be simple objects.

var MyMixin0 = {
  mixin0Attr: "Mixin 0 Attribute Value"
};


var MyMixin1 = {
  mixin1Attr: "Mixin 1 Attribute Value",
  mixin1Func: function() {
    if (typeof this.protoAttr0 !== 'undefined') {
      console.log("Mixin 1 function hit!");
      console.log("  this.protoAttr0 = '" + this.protoAttr0 + "'");
    }
  }
};


var MyMixin2 = {
  mixin2Func: function() {
    console.log("Mixin 2 function hit!")
  }
};




var MyNewObject = ObjectDefinition.create({

  mixins: [
    MyMixin0,
    MyMixin1,
    MyMixin2
  ],


  static: { // These attributes will NOT be inherited by any child object definitions.
    staticAttr0: 42,
    staticAttr1: 4242,

    staticFunc0: function(param0) {
      alert("MyNewObject.staticFunc0 hit!");
    }
  },


  protoAttr0: null,
  protoAttr1: "Fish fingers and custard",
  protoAttr2: {
    attr0: 17,
    attr1: null
  },
  protoAttr3: [ 'val0', 'val1', 'val2' ],



  // This function is NOT required, but it acts as the constructor
  // for object creation if is present.
  ctor: function MyNewObject(id) {
    console.log("MyNewObject.ctor hit!");
    this.protoAttr0 = id;
  },
    

  func0: function() {
    console.log("MyNewObject.func0 hit!");
  },


  func1: function(toPrint) {
    console.log(toPrint);
  }
});



console.log("MyNewObject.name = '" + MyNewObject.name + "'");

console.log("MyNewObject.staticAttr0 = '" + MyNewObject.staticAttr0 + "'");
console.log("MyNewObject.staticAttr1 = '" + MyNewObject.staticAttr1 + "'");


MyNewObject.staticFunc0();


var obj = new MyNewObject(4400);
console.log("obj.protoAttr0 = '" + obj.protoAttr0...