How to refer to an object created by a function in an object?
-
Good day,
I simulate the database, creating one object for each new request. There was a problem that I cannot access the properties of the object, it throws undefined. or can't read property of undefined. For example, I want to display only timings for all objects, or only for one specific one, but it turns out like this. Also don't understand yet how to refer to a specific object in an object, it also throws undefined. There is an assumption that it can be solved somehow through loops, but I don't know how. Or am I just calling the properties I need in the wrong way?
My code:
let ger_task = {} let name = 1; let descr = 2; let timing = 3; let ready =4; let auth = 5; //функция для создания новых заданий или объектов function newTask(name,descr,timing,auth,ready) { this.name = name; this.descr = descr; this.timing = timing; this.ready = ready; this.auth = auth; content = { task:this.name, description:this.descr, timings:this.timing, author:this.auth, readiness:this.ready, } ger_task[name] = this.content ; } newTask('1', '2', "3",'4', "5") newTask('а', 'b', "c",' d', "e") console.log(ger_task.content.task)
Thanks for the help!JavaScript Anonymous, Nov 18, 2019 -
Ger_task doesn't have a content property.
console.log (ger_task.а.task);Aria Strong -
// this program shows how to access an element in an object via the object method
let ger_task = {
name: [1,2,3,4],
descr: [1,2,3,4],
meth: function (value) {
console.log (this.name [2]);
console.log (this.descr [1]);
// when the function is a method of an object then the this pointer will be bound to it,
// if the function belongs to the global scope, this points to the window object
return value
}
};
console.log (ger_task.meth (ger_task.name))
console.log (ger_task.meth (ger_task.descr [3]))Adelaide Harding
2 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!