Skip to content

Instantly share code, notes, and snippets.

@nmtitov
Created November 15, 2012 22:33
Show Gist options
  • Select an option

  • Save nmtitov/4081895 to your computer and use it in GitHub Desktop.

Select an option

Save nmtitov/4081895 to your computer and use it in GitHub Desktop.

Revisions

  1. nt9 created this gist Nov 15, 2012.
    30 changes: 30 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    // конструктор родительского класса
    var A = function () {
    this.x = 10;
    this.y = 10;
    this.area = function () {
    return this.x * this.y;
    }
    }

    // A.prototype.area = function () {
    // return this.x * this.y;
    // }

    var a = new A();
    console.log('area of super class', a.area());

    var B = function () {
    A.call(this); // вызываем конструктор родителя
    }

    B.prototype = Object.create(A.prototype); // наследуем, т.е. используем прототип родителя

    // меняем имплементацию метода в родителе

    A.prototype.area = function () {
    return Math.pow(this.x, this.y);
    }

    var b = new B();
    console.log('area of child class', b.area());