Why can Errors not be stringified? [duplicate] - javascript

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"}"

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);
}

Uncaught Typeerror: cant read properties of (undefined) reading 'message' [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 10 months ago.
I'm making a simple library which turns JS objects into JS class componeents to register and use in HTML. I'm testing it with a simple object:
const myEle = {
state: {
message: `Hello World`
},
template: `<p> ${this.state.message} </p>`,
style: {
color: "red"
},
hooks: {
onClick: () => alert("Hi!")
},
attrs: {},
element: toComp(this.template, this.style, this.state, this.hooks, this.attrs)
}
Specifically, the error is with template. which gives me the error mentioned in the title, despite this.state.messagemaking snese ti ne. Why is that happening?
You can't access the object during its initialization with object literal syntax.
This is invalid javascript:
const obj = {
property1: 'hello',
property2: property1
};
You can access it after its initialization:
const obj = {
property1: 'hello',
};
obj.property2: property1;

Creating usual object using Object.create? [duplicate]

This question already has answers here:
Why are properties not shown when using Object.create() and console.log()?
(2 answers)
why console.log doesn't show values of properties added by PROTOTYPES in javascript when whole object is printed?
(2 answers)
Closed last year.
let animal = {
eats: true
};
let rabbit = Object.create(animal, {
jumps: {
value: true,
writable: true,
configurable: true
}
});
console.log(rabbit.__proto__); // logs: {eats: true};
console.log(animal); // logs: {eats: true};
console.log(rabbit.jumps); // logs: true;
console.log(rabbit); // logs {}
The question is: if rabbit is empty for real, what happend?
animal - has no jumps property
animal.proto - has no jumps property
rabbit - has no jumps property
rabbit.jumps - is true
Your new property is not enumerable - if it were it would show up in a text-based console (Your original code shows up in an object-based console like chrome for example):
const animal = {eats:true}
let rabbit = Object.create(animal, {
jumps: {
value: true,
writable: true,
configurable: true ,
enumerable:true
}
});
console.log(rabbit.__proto__); // logs: {eats: true};
console.log(animal); // logs: {eats: true};
console.log(rabbit.jumps); // logs: true;
console.log(rabbit); // logs as you expected
From the docs for Object.defineProperties():
enumerable
true if and only if this property shows up during enumeration of the properties on the corresponding object. Defaults to false.

Javascript Gives confusing output [duplicate]

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

How to fetch data Array in Promise [duplicate]

This question already has answers here:
Why does .json() return a promise?
(6 answers)
Closed 6 years ago.
I call API on my react JS app :
result = fetch('http://localhost:5000/cities')
result.then(function(response) {
console.log(response.json());
})
Then output log :
Promise
__proto__ : Promise
[[PromiseStatus]] : "resolved"
[[PromiseValue]] : Array[5]
0: Object
1: Object
2: Object
3: Object
4: Object
length: 5
__proto__: Array[0]
How do I fetch data on Array ?
I need get city name list
The line response.json() returns a promise that resolves to the parsed JSON.
You can simply write
result.then(function(response) {
return response.json();
}).then(function (data) {
console.log(data);
});

Categories

Resources