Created
May 26, 2012 07:39
-
-
Save yukop/2792791 to your computer and use it in GitHub Desktop.
#javascript #codecademy Self review of Introduction to Objects I & II
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 円の半径を渡していろいろする | |
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