Skip to content

Instantly share code, notes, and snippets.

@benqus
Created January 8, 2013 12:20
Show Gist options
  • Select an option

  • Save benqus/4483318 to your computer and use it in GitHub Desktop.

Select an option

Save benqus/4483318 to your computer and use it in GitHub Desktop.

Revisions

  1. Bence created this gist Jan 8, 2013.
    58 changes: 58 additions & 0 deletions js_inheritance
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    var prototypeTest = (function () {
    /**
    * Instance class definition
    * inherits from Base
    */
    function Instance() {
    //super class constructor
    Base.apply(this, arguments);
    }

    /**
    * Base class definition
    */
    function Base(name) {
    var privates = {
    'name': name
    };

    this.getPrivate = function (name) {
    return privates[name];
    };
    }

    /**
    * prorotype function
    */
    Base.prototype.get = function () {
    return this.getPrivate.apply(this, arguments);
    };

    /**
    * resolving inheritance in the scope
    */
    Instance.prototype = Base.prototype;

    /**
    * exposing 2 instances
    */
    return {
    i: new Instance('Geri'),
    j: new Instance('Benő')
    };
    }());

    /**
    * short logging to check behaviour
    */
    console.log(Object.getPrototypeOf(prototypeTest.i));
    console.log(Object.getPrototypeOf(prototypeTest.i).set = function () {});

    console.log(Object.getPrototypeOf(prototypeTest.i) === Object.getPrototypeOf(prototypeTest.j));
    console.log(Object.getPrototypeOf(prototypeTest.j));
    console.log(Object.getPrototypeOf(prototypeTest.i));

    console.log(prototypeTest);

    console.log(prototypeTest.i.get('name'));
    console.log(prototypeTest.j.get('name'));