Skip to content

Instantly share code, notes, and snippets.

@seysdev
Created June 20, 2019 03:30
Show Gist options
  • Select an option

  • Save seysdev/59b14c7cc3d820496d46dc2c1c03edca to your computer and use it in GitHub Desktop.

Select an option

Save seysdev/59b14c7cc3d820496d46dc2c1c03edca to your computer and use it in GitHub Desktop.

Revisions

  1. @blessebas blessebas created this gist Jun 20, 2019.
    37 changes: 37 additions & 0 deletions prototype.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    var myCar = {
    name: "Ford Escort",
    drive: function () {
    console.log( "Weeee. I'm driving!" );
    },
    panic: function () {
    console.log( "Wait. How do you stop this thing?" );
    }
    };

    // With object create
    var yourCar = Object.create(myCar);


    // with fn prototype
    function MyCar() {}
    MyCar.prototype.name = "Ford Escort";
    MyCar.prototype.drive = function () {
    console.log( "Weeee. I'm driving!" );
    };
    MyCar.prototype.panic = function () {
    console.log( "Wait. How do you stop this thing?" );
    }

    const mycar = new MyCar();

    // other way
    function MyCar() {}
    MyCar.prototype = {
    name: "Ford Escort",
    drive: function () {
    console.log( "Weeee. I'm driving!" );
    },
    panic: function () {
    console.log( "Wait. How do you stop this thing?" );
    }
    };