Giter Club home page Giter Club logo

dog.js-001's Introduction

Dog.js

Represent a dog as an object. Your dog has two properties, name and age. There should be three functions that represent this age in years, days, and dog years (every year is 7 dog years).

The three functions should be attached to the prototype while the name and age properties should be attached to each new object.

Properties defined on the instance Vs. Functions defined on the prototype

Prototypes in JavaScript are what store 'instance methods', which is different than classical inheritance in Ruby. In Ruby a new instance copies all the instance methods it knows upon creation and stores them for access.

JavaScript instances do not store any methods from their prototype; instead, they will refer back to prototype every time the method is called. This is much more memory efficient, but has the draw back of being less clear.

This means that every new instance of a prototype share one copy of a method defined on the prototype. This also means that every instance shares the same property if it is defined on the prototype. A property defined on the prototype can be though of more like a Ruby constant.

ex.

// JavaScript constructor,
// like a Ruby class/initialize method all in one
function Person(){
};

// like a Ruby instance method
Person.prototype.greet = function(){ 
  return 'Hi!!!';
};

var avi = new Person();
var arel = new Person();

// Same function, same return value
avi.greet();
  // -> 'Hi!!!'
arel.greet();
  // -> 'Hi!!!' 

// Like a Ruby constant
Person.prototype.SPECIES = 'Homo Sapien';

// Same property, same return value
avi.SPECIES
  // -> 'Homo Sapien'
arel.SPECIES
  // -> 'Homo Sapien'

To have information specific to an instance use the this keyword, like Ruby's self to create new properties of the instance inside the constructor function.

ex.

function Person(catchPhrase){
  // A JavaScript property
  // inside a constructor this property behaves
  // like a ruby attribute accessor
  this.catchPhrase = catchPhrase;
};

var avi = new Person("That's so freakin' cool!");
var arel = new Person("Boats!");

avi.catchPhrase;
  // -> "That's so freakin' cool!"
arel.catchPhrase;
  // -> "Boats!"

As a note (and hint), this can be accessed inside prototypical methods too. However, unlike self this is explicit, meaning it must be stated every time you want to use it.

ex.

function Person(catchPhrase){
  this.catchPhrase = catchPhrase;
};

Person.prototype.denyCatchPhrase = function(){
  return "What!?! I like never say, '" + this.catchPhrase + "'";
};

var avi = new Person("That's so freakin' cool!");
var arel = new Person("Boats!");

avi.denyCatchPhrase();
  // -> "What!?! I like never say, 'That's so freakin' cool!'"
arel.denyCatchPhrase();
  // -> "What!?! I like never say, 'Boats!'"

To run the specs follow these commands:

# first install the new gem to run the tests
gem sources -a http://flatiron:[email protected]
gem install learn-co

# to run in the command line run
learn

#to run in the browser
learn -b

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.