Javascript Gives confusing output [duplicate] - javascript

This question already has answers here:
Console.log showing only the updated version of the object printed
(3 answers)
Setting a variable equal to another variable [duplicate]
(3 answers)
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 3 years ago.
I am new to Javascript, but have Java experiance.
const person = {
name: "bin",
walk() {
console.log(this);
}
};
person.walk();
const per1 = person;
per1.name = "cin";
per1.walk();
const animal = {
type: "2",
m2() {
console.log("type");
}
};
const walk1 = person.walk.bind(animal);
walk1();
person.walk();
I am failing to understand the output at line number 1 which prints a value of cin for name. When the code executes at person.walk(), the value of name has not changed. Why is the value being printed as cin?
{name: "bin", walk: ƒ}name: "cin"walk: ƒ walk()__proto__: Object
{name: "cin", walk: ƒ}name: "cin"walk: ƒ walk()__proto__: Object
{type: "2", m2: ƒ}type: "2"m2: ƒ m2()__proto__: Object
{name: "cin", walk: ƒ}name: "cin"walk: ƒ walk()__proto__: Object

Related

What's wrong with this method? the .push() method isn't working as expected [duplicate]

This question already has answers here:
Array.push return pushed value?
(7 answers)
Closed 4 months ago.
const student1 = {
id: 1,
name: "Reed",
subjects: [],
addSubject(subject) {
this.subjects = this.subjects.push(subject); //what's wrong with this line
}
}
student1.addSubject('Math');
console.log(student1.subjects);
// logs out 1 instead of ['Math'], .push isn't functioning properly
const student1 = {
id: 1,
name: "Reed",
subjects: [],
addSubject: function(subject) {
this.subjects.push(subject);
}
}
student1.addSubject('Math');
console.log(student1.subjects);
Array.push() returns the new length of the array, not the array itself.
Unless you have a reason to capture this value, you don't need to assign it:
addSubject(subject) {
this.subjects.push(subject);
}

how does usage of braces change the value in javascript? [duplicate]

This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
Javascript object literal: what exactly is {a, b, c}?
(3 answers)
Closed last year.
I have the below Javascript code:
const product = await productsRepo.getOne(req.params.id);
console.log(product);
console.log({product});
Output from first console.log.
{ title: 'hey12up', price: 1234, image: '', id: 'b6ff5da7' }
Output from second console.log:
{
product: { title: 'hey12up', price: 1234, image: '', id: 'b6ff5da7' }
}
Though I understand that in the second output, product is a key and the value is an object (which are further key-value pairs), I dont understand how this conversion is done?
Just by enclosing the object name "product" inside braces, what is happening? Is there any technical term for this?

loop through array of objects using ramda [duplicate]

This question already has answers here:
How to use Ramda to find matching object in Array by key value
(2 answers)
How to find first element of array matching a boolean condition in JavaScript?
(14 answers)
Closed 3 years ago.
I have a array of objects this way.
0:
cardLast4Digits: "0664"
cardType: "GIFT_CARD"
__proto__: Object
1:
cardLast4Digits: "5551"
cardType: "CREDIT_CARD"
__proto__: Object
I want to loop through this array of object and find if cardType is "GIFT_CARD". Once i find it, i want to get that object as a result. Output should then be
0:
cardLast4Digits: "0664"
cardType: "GIFT_CARD"
__proto__: Object
Can someone please suggest me how to do this withv ramda.
Just use the array find method: https://ramdajs.com/docs/#find
Ramda:
const items = [{
cardLast4Digits: '0664',
cardType: 'GIFT_CARD'
}, {
cardLast4Digits: '5551',
cardType: 'CREDIT_CARD'
}];
R.find(R.propEq('cardType', 'GIFT_CARD'))(items);
ES6:
const items = [{
cardLast4Digits: '0664',
cardType: 'GIFT_CARD'
}, {
cardLast4Digits: '5551',
cardType: 'CREDIT_CARD'
}];
const result = items.find(item => item.cardType === 'GIFT_CARD');
console.log(result);

Combine array and object destructuring [duplicate]

This question already has answers here:
Array destructuring in JavaScript
(5 answers)
Closed 3 years ago.
How to destruct a value from array?
const ?? = { text: ['some text'] };
const { text: [someText]} = { text: ['some text'] };
console.log(someText);

Why can Errors not be stringified? [duplicate]

This question already has answers here:
Is it not possible to stringify an Error using JSON.stringify?
(14 answers)
Closed 7 years ago.
Why can Errors not be stringified?
JSON.stringify(new ReferenceError('foo')); // {}
When for example, Date does something more useful:
JSON.stringify(new Date()); // "2015-04-01T10:23:24.749Z"
JavaScript Error objects are not enumerable. You can verify this easily:
new Error('Test').propertyIsEnumerable('message');
// -> false
You can however define your own toJSON function on the error Object:
Object.defineProperty(Error.prototype, 'toJSON', {
value: function () {
return {value: "Test"};
},
configurable: true
});
JSON.stringify(new Error());
-> "{value: "Test"}"

Categories

Resources