Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
  <head>
<meta name="description" content="Inheritance.js 0.1.1 Sample: Create New Object Definition">
    <meta charset="utf-8">
    <title>Inheritance.js 0.1.1 Sample: Create New Object Definition</title>
    <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>
  </head>
  <body>
    <h1>Inheritance.js 0.3.2 Sample: Create New Object Definition</h1>
    <p>Open the "JavaScript" and "Console" panels for details.</p>
  </body>
</html>
 
// =============================================== //
// 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 + "'");
console.log("obj.protoAttr1 = '" + obj.protoAttr1 + "'");
console.log("obj.protoAttr2.attr0 = '" + obj.protoAttr2.attr0 + "'");
console.log("obj.protoAttr2.attr1 = '" + obj.protoAttr2.attr1 + "'");
console.log("obj.protoAttr3 = '" + obj.protoAttr3 + "'");
obj.func0();
obj.func1("This is what I want printed.");
console.log("obj.mixin0Attr = '" + obj.mixin0Attr + "'");
console.log("obj.mixin1Attr = '" + obj.mixin1Attr + "'");
obj.mixin1Func();
obj.mixin2Func();
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
bsarapro
0viewers