0
function getPercCalculated(x){
  return (x*9.3)/100;
}

var x = 10;
var perx = getPercCalculated(x);

Instead of this I would like to call the getPercCalculated using dot operator like

var perx = x.getPercCalculated()

Can someone help me..!!

Anish Joseph
  • 1,026
  • 3
  • 10
  • 24
  • 1
    Why would you need this? – Yury Tarabanko Jul 21 '17 at 09:15
  • It is possible, but remember **it is a very bad practice** https://stackoverflow.com/questions/14034180/why-is-extending-native-objects-a-bad-practice – Tamas Hegedus Jul 21 '17 at 09:15
  • 1
    To do this you need to extend the [**Number.prototype**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/prototype) with this method, which is not very recommended as it will affect all created numbers. You will need to use: `Number.prototype.getPercCalculated = function (){ return (this*9.3)/100; }` – cнŝdk Jul 21 '17 at 09:17

1 Answers1

1
Number.prototype.getPercCalculated= function(){
    return (this*9.3)/100;
};

This will attach the getPercCalculated to every number in your code tough

Ivan Mladenov
  • 1,787
  • 12
  • 16