This question already has answers here:
Does JavaScript pass by reference? [duplicate]
(13 answers)
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 2 years ago.
I'm new to Javascript
The function is not changing the outer variable
function edit(array, letter){
array = 0;
letter = 'b';
}
let string = 'a';
let array = [1,0,2];
edit(array, string);
console.log(array, string);// Result [1,0,2] not 0 b
Related
This question already has answers here:
Javascript How to define multiple variables on a single line?
(10 answers)
What is the purpose of the var keyword and when should I use it (or omit it)?
(19 answers)
Closed 2 months ago.
Can any one explain why this happens?
// Example 1
(function () {
var a = b = 5;
})();
console.log(b); // Output 5
var a = b = 5 and var b = 5 is totally different?
This question already has answers here:
Self-references in object literals / initializers
(30 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 11 months ago.
I'm learning javascript, I'm new to programming and i was wondering about "this".
let someObject = {
a : "happy",
b : "sad",
c : function(){
return this.a
},
d : this.a
}
someObject.c() returns "happy" but someObject.d returns undefined. why can't it simply assign the value of 'a' to 'd'?
This question already has answers here:
Add method to string class
(6 answers)
Closed 3 years ago.
I have this sample function, and i want to call it in this format :
const myfunc = (string) => {
return string.length
}
console.log("check_length".myfunc())
How can i do that?
Thanks for answers in advance!
The only way is to mutate the prototype of String with a classic function for accessing this.
String.prototype.myfunc = function () {
return this.length;
};
console.log("check_length".myfunc());
This question already has answers here:
How does "this" keyword work within a function?
(7 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 6 years ago.
Nope, the title is not an enigma.
I have :
obj = {
NaNException:function(message=''){
this.message=message;
},
input:{
integer:function(a){
var b = prompt(a);
if (isNaN(parseInt(b, 10)))
throw new this.NaNException();
else
return b;
}
}
}
so, in NaNException:function(){ }, this is expected to be NaNException, but in integer, it is expected to be obj... which interpretation is the good one? And what can I do with the wrong one to have the expected result ?
Thanks in advance.
This question already has answers here:
What's wrong with var x = new Array();
(5 answers)
What is the difference between condensed arrays and literal arrays?
(3 answers)
Closed 6 years ago.
Is typing "var foo = []" the same as typing "var foo = new Array()"?