Calling javascript function using dot operator [duplicate] - javascript

This question already has answers here:
How to observe value changes in JS variables
(3 answers)
Closed 5 years ago.
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..!!

Number.prototype.getPercCalculated= function(){
return (this*9.3)/100;
};
This will attach the getPercCalculated to every number in your code tough

Related

Is it possible to create a ES6 Javascript expression whose value is an object with a dynamic property name? [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 4 years ago.
I currently do:
function outer(prop_name) {
const tmp = {};
tmp[prop_name] = 'hello world';
foo(tmp);
}
Is there a way of rewriting this as:
foo(<expression>)
using an expression involving prop_name?
You can write it as
foo({ [prop_name] : 'hello_world'});

Evaluating expressions and parentheses in JavaScript [duplicate]

This question already has answers here:
How is division before multiplication in Javascript?
(2 answers)
Closed 4 years ago.
I'm having trouble understanding how JS is processing my expressions..
This evaluates incorrectly and drops the denominator:
var a = document.getElementById("Acceleration").value;
var u = document.getElementById("Initial_Velocity").value;
var v = document.getElementById("Final_Velocity").value;
document.getElementById("Distance").value = (v*v-u*u)/2*a;
This evaluates properly:
document.getElementById("Distance").value = (v*v-u*u)/(2*a);
I'm using Dreamweaver. Thanks.
Well the formulas are different. You left out the parentheses (2*a) in the first code sample.

Javascript - Function Not Changing Global Variable [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 4 years ago.
My Code (Javascript):
var num = 0;
function change(){
var num = 10;
}
change();
document.write(num);
The result should be 10 but it is showing 0. Why?
If the code is wrong, what is the correct way to do this?
You are setting another local variable called num.
Just change the inner var num to num.

Create a variable named after a string parameter in JavaScript [duplicate]

This question already has answers here:
Convert string to variable name in JavaScript
(11 answers)
Closed 8 years ago.
I know this piece of code is wrong, but I wonder if there is any way this would be possible.
var createVariableFromParameter = function(myName) {
var myName = [];
return myName;
}
Thank you.
EDIT: I found the solution here: Convert string to variable name in Javascript
I think it's not possible, and even if it could be done, why would you want to do it? There is no meaning in creating variables with random names...
I don't know why you should need it??
But I think true way is :
function create(myvar) { myvar = [];}
var a;
create(a);
function create() { return [];}
var b = new create();

How insert variable in JS [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 9 years ago.
I need help, how to insert variable n as XX. Variable n is number, for example 3 and I need to get this number to document.form1.any3.value; is it possible? Thanks for advice
function pokus(n){
var any1 = document.form1.anyXX.value;
}
Use square bracket notation:
function pokus(n){
var any1 = document.form1['any'+n].value;
}

Categories

Resources