This question already has answers here:
Is it possible to implement equal operator in ES6?
(1 answer)
How to overload operator equality for JavaScript objects
(3 answers)
Closed 2 months ago.
I am trying to override valueOf and toString
This is becouse I want to add some properties to the premitive values
So I created this below
class Box {
#value;
constructor(value) {
this.#value = value;
}
valueOf() {
return this.#value;
}
toString(){
return this.#value;
}
someMethod(){
return "something else"
}
}
const t1 =21;
const t2 = 21
const p = new Box(45);
const s = new Box(45);
console.log(p === s, t1 === t2)
notice that t1 and t2 comparesion works great but p and s are not working as expected
Why is that and is there a way to do the thing I want?
Related
This question already has answers here:
How can I use default parameters when no value are passed in creating a new object?
(1 answer)
How to destructure option argument with all default values in ES6?
(1 answer)
Closed 11 months ago.
I would like to create a function that takes one optional property called verbose, and if it's not specified it defaults to true. Here is an example:
function Counter(n=0, {verbose=false}) {
return function() {
n++;
if (verbose) console.log(n);
return n;
}
}
let c1 = Counter(0, {verbose: true});
c1(), c1(), c1();
Everything works fine. However, as soon as I call it without the object param, I get an error:
function Counter(n=0, {verbose=false}) {
return function() {
n++;
if (verbose) console.log(n);
return n;
}
}
let c1 = Counter(0);
// TypeError: Cannot read property 'verbose' of undefined
Why does this occur, as I thought the whole point of having an object that can be destructured in the argument list is the suggested way to have a bunch of optional arguments that can be called. What am I doing wrong here?
Don't put the default in the destructuring pattern, put it in a default value that's assigned to the whole object.
function Counter(n=0, {verbose} = {verbose: false}) {
console.log(verbose);
return function() {
n++;
if (verbose) console.log(n);
return n;
}
}
let c1 = Counter(0);
let c2 = Counter(0, {verbose: true});
This question already has an answer here:
Type annotation for `this` keyword in Typescript
(1 answer)
Closed 2 years ago.
Having this getCurrentTime function:
const getCurrentTime = () => {
const date = new Date();
const d = Date.prototype;
const funcs = [
d.getFullYear,
function() {
return d.getMonth.call(this) + 1;
},
d.getDate,
d.getHours,
d.getMinutes,
];
return funcs.reduce(
(prevStr, current) => `${prevStr}-${current.call(date)}`,
'',
);
};
Raised an error: 'this' implicitly has type 'any' because it does not have a type annotation
I tried
return d.getMonth.call<Date, any[], number>(this as Date) + 1;
return d.getMonth.call(this as Date) + 1;
Neither worked, how can I fix this error
I agree with the comments that simply doing date.getMonth() + 1 would be better.
As for the actual TypeScript error, though, adding explicit typing for this helps and can be done similarly to explicit argument types
function(this: Date) {
return d.getMonth.call(this) + 1;
}
"this parameters" in docs
This question already has answers here:
"add" function that works with different combinations of chaining/arguments
(4 answers)
Closed 4 years ago.
I have been looking for a way to create an 'add' function such that :
add(1,2) //returns 1+2= 3
add(1)(2,3)(4) // returns 10
add(1)(2,3)(4)(5,6)(7,8,9) //returns 45
I am able to create add method if I know the number of arguments we have, for e.g.:
const add5 = a => b => c => d => e => a+b+c+d+e;
So, if I used add5(1)(2)(3)(4)(5), this will give me the expected output.
But the problem is how to tackle the problem if we have to return sum of 'N' parameters.
TIA !
It's not possible in the general case unless toString coercion is allowed on the result of calling add (or unless the number of calls is known in advance):
function add(...next) {
let count = 0;
// return a callable function which, when coerced to a string,
// returns the closure's `count`:
function internalAdd(...next) {
count += next.reduce((a, b) => a + b, 0);
return internalAdd;
}
internalAdd.toString = () => count;
return internalAdd(...next);
}
console.log('' + add(1,2)) //returns 1+2= 3
console.log('' + add(1)(2,3)(4)) // returns 10
console.log('' + add(1)(2,3)(4)(5,6)(7,8,9)) //returns 45
This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
Closed 4 years ago.
Why it returns false?
let a = new Object()
let b = Object()
console.log(a) // {}
console.log(b) // {}
console.log(a===b) // false
I checked a proto of a and b too and it is the same.
So what is the difference?j
Instance of objects are not the same even:
let a = new Object();
let b = new Object();
console.log(a===b) // false
This question already has answers here:
Determine original name of variable after its passed to a function
(9 answers)
JavaScript: Get Argument Value and NAME of Passed Variable [duplicate]
(7 answers)
Closed 7 years ago.
I know this is completely useless, but I'm nonetheless curious. Is it possible to do something like the below?
function getVariableName ( v )
{
// .... do something .....
// return the string "v"
}
var x = 69;
var y = "somestring";
console.log(getVariableName(x)); // prints "x" to the console
console.log(getVariableName(y)); // prints "y" to the console
function getArgNames(fn) {
var matches = fn.toString().match(/\(([a-z_, ]+)\)/i);
if (matches.length > 1) {
return matches[1].replace(/\s+/g, '').split(',');
}
return [];
}
That will return an array of the names of all arguments passed in to the function.