-1
function CircleArea(Value) {
   var Results = 3.14 * Value^2;
   return Results;
}

So I want:

var Diameter = 30;
Diameter.CircleArea(); // Results 2826

Like:

var n = 30; n.toString() // Results 30

Syntax:

varName.myFunction();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Are you asking how to add a function to a number? – Mike Cluck May 23 '16 at 21:28
  • 1
    Welcome to StackOverflow. Please have a look at some helpful tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) - asking a good question improves your chances of getting an answer. Also look at [Minimal, Complete, and Verifiable Examples.](http://stackoverflow.com/help/mcve) – ishmaelMakitla May 23 '16 at 21:41

2 Answers2

2

You can but rarely should extend prototypes:

Number.prototype.circleArea = function() {
  var value = this;
  return Math.pow(value, 2) * Math.PI;
};

var num = 30;
console.log(num.circleArea()); // 2827.4333882308138
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • @pro2014learning I think I should have emphasized `but rarely should` See: https://stackoverflow.com/questions/14034180/why-is-extending-native-objects-a-bad-practice – Andreas Louv May 23 '16 at 21:33
0

Your function accepts an argument .

So you will need to pass in Diameter like so :

CirclArea(Diameter)

Returns 2826

KpTheConstructor
  • 3,153
  • 1
  • 14
  • 22