pro2014learning pro2014learning - 2 months ago 20
Javascript Question

How can javascript like varName.myFunction()

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();

Answer

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
Comments