This question already has answers here:
Add method to string class
(6 answers)
Closed 4 years ago.
In javascript, I want to write a function which is called as follows:
var x = 'testString'
var y = 'anotherstring'
var z = 0
var result = x.aFunction(y, z)
function aFunction(y, z) {
...
}
This is the first time I am attempting this, my question is how can I get and use the value of x in the function aFunction, without actually referring to the declared variable.
I tried looking for this but I cannot find anything. If there is a post specifically for this that anyone knows about, please let me know.
Thanks
You need to use String.prototype.aFunction so that you can add a custom function aFunction() to the prototype of String such that it can be invoked by a string variable. Also this.toString() inside the prototype function will give you the value of the x variable (calling string)
var x = 'testString'
var y = 'anotherstring'
var z = 0
String.prototype.aFunction = function(y, z){
console.log(this.toString());
return y+z;
}
var result = x.aFunction(y, z);
console.log(result);
Related
This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 1 year ago.
Basically why I can't do this:
var a = [];
var b = a.push;
b(1);
it give's error, I know how to avoid this, but why this is not working in js?
The this value is determined by how the function is called. You can use Function#bind to ensure the this value is set.
var a = [];
var b = a.push.bind(a);
b(1);
console.log(a);
Try this:
var a = [];
var b = (param) => a.push(param);
b(1);
This question already has an answer here:
Scope of Default function parameters in javascript
(1 answer)
Closed 4 years ago.
ES6 introduced default parameters. I'm trying to understand how inline function default parameters work with this new feature. Specifically how its scoping work.
Take for example the following two functions:
function one(x, f = function (){return x})
{
var x = 5;
console.log([x,f()]);
}
function two(x, f = function (){return x})
{
x = 5;
console.log([x,f()]);
}
one(1);//[5,1]
two(1);//[5,5]
Is it correct to say that, in function one, f keeps it's own closure scope for x in the parameter list, so that when the function redefines x as a new var: var x = 5;, the reference that f has, is not the same as the one inside the function?
If that's the case, is function one equal to function three below:
function three(x,f)
{
var x2 = x;
f = f !== undefined ? f : () => x2;
var x = 5;
console.log([x,f()]); //[5,1]
}
I tried, without luck, to find how this behavior is documented, if someone could point me to the right part of the documentation that would also be great.
You are correct. In both of the top functions x in f refers to the parameter x.
There's some considerations with case 3.
In the third example if f isn't defined and you are returning x2 when calling the function it will be equal to whatever x originally was. When you do x = 5; you aren't changing x2. This is because when you assign x2 = x JavaScript makes a copy not a reference.
Unless the x parameter is passed an array or object x2 will be a copy and not a reference of x.
So if you do three(3) then x2 will always be 3 because you're never changing it.
This question already has an answer here:
Where can I get info on the object parameter syntax for JavaScript functions?
(1 answer)
Closed 5 years ago.
const f = ({x,y})=>{...do something...};
const f = (x,y)=>{...do something...};
What is the difference between these two lines ?
My understanding is both pass "x" and "y" are arguments.
Are both the same then ?
The first one uses destructuring you can use it as
f({x: 0, y: 1})
you pass one object with fields x and y
and then in body you can access fields of object as variables x and y.
In the second one you have to pass 2 variables
f(0, 1)
Bodies can be the same of those functions and they would work analogically
for instance
=> { return x + y; }
but params should be passed in different ways
The first one you're passing an object as a parameter. But I don't think it's a working example. Here is one
class O {
x;
y;
}
const example =(o:O)=>{
//do something
return o.x+o.y;
};
The equivalent is
var example = function example(o) {
//do something
return o.x+o.y;
};
The second one you're passing two parameters the equivalent is
var example2 = function example2(x, y) {
//do something
return 0;
};
This question already has an answer here:
Trouble referencing variable in Collections.where method within render function
(1 answer)
Closed 9 years ago.
Can someone point me in the right direction? How do you pass a reference variable in to an object literal in JavaScript? I am using Backbone.js and specifically I am using the collections.where method. So I have something as follows:
var temp = customers.where({num: 10});
However, what if someone has a variable like var x (that changes) and they want to say something like the following:
var temp = customers.where({num: x});
JavaScript won't let you do this, I know. But how is it done or how do you get around it?
You create a closure over x like this:
var x = 10;
var filter = function() { return customers.where({num: x}); };
var temp = filter(); // uses x = 10
x = 20;
temp = filter(); // uses x = 20
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get variable name. javascript “reflection”
Is there a way to know the name of a variable?
Example:
var a = 1;
var b = 4;
function getName(param){
//What should I return here?
}
What I want to do, is to have getName return "a" if I call getName(a)and return "b" if I call getName(b)
Is this possible?
No, that's not possible in a clean way and I highly doubt there is a useful use-case for this.
You could alter the prototype of the object to add a function to do this, as described in this StackOverflow answer:
Object.prototype.getName = function() {
var funcNameRegex = /function (.{1,})\(/;
var results = (funcNameRegex).exec((this).constructor.toString());
return (results && results.length > 1) ? results[1] : "";
};
It isn't possible to get variable name. But you can get name of variable, who got exactly this value (if you got some vairables with equal values, you get first defined). And this function works only for global variables.
var a = 1;
var b = 4;
function test(value) {
for (var x in window)
if (window[x] === value)
return x;
}
alert(test(b));