Skip to content

Instantly share code, notes, and snippets.

@yukop
Created May 26, 2012 07:39
Show Gist options
  • Save yukop/2792791 to your computer and use it in GitHub Desktop.
Save yukop/2792791 to your computer and use it in GitHub Desktop.
#javascript #codecademy Self review of Introduction to Objects I & II
// 円の半径を渡していろいろする
var Circle = function(radius) {
 this.radius = radius;
 this.diameter = radius * 2;
};
Circle.prototype.getPerimeter = function() {
 return this.radius * Math.PI * 2;
};
Circle.prototype.getArea = function() {
 return this.radius * this.radius * Math.PI;
};
// 以下のようにも書けるよ
// Circle.prototype = {
//  getPerimeter: function() {
//   return this.radius * Math.PI * 2;
//  },
//  getArea: function() {
//   return this.radius * this.radius * Math.PI;
//  }
// };
// circleA の円周と circleB の円周の合計を出すよ
var circleA = new Circle(10);
var circleB = new Circle(15);
console.log(circleA.getPerimeter() + circleB.getPerimeter());
// circleA と circleB は getPerimeter メソッドを持っていないことを確認
console.log(circleA.hasOwnProperty('getPerimeter'));
console.log(circleB.hasOwnProperty('getPerimeter'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment