#Question about `Object.assign()`

15 messages · Page 1 of 1 (latest)

vapid rune
#

okay so I'm trying to shorten

var spells_function = {
    "Empty": function () {
        alert("Empty Spell")
    },
    "Shield": function () {
        alert("Shield Spell")
    },
    "Charged Beam": function () {
        alert("Charged Beam Spell")
    },
    "Movment": function () {
        alert("Movment Spell")
    },
    "Invisible": function () {
        alert("Invisible Spell")
    },
    "Telekinesis": function () {
        alert("Telekinesis Spell")
    },
    "Tracker": function () {
        alert("Tracker Spell")
    }
}

to

for (var i = 0, spells =["Empty", "Shield", "Charged Beam", "Movment", "Invisible", "Telekinesis", "Tracker"]; i < 7; i++) {
    Object.assign(spells_function, {spells[i]: function () {
            alert(spells[i] + " spell")
        }
    });
}```
as far as i can understand it its not working because of line
`Object.assign(spells_function, {spells[i]: function () {`
and the `[i]` part of it I've tried  `String(spells[i])`
now the issue here is i don't know to much about java script like i understand some basic stuff which I'm still learning(note: I'm not doing web development or anything like that I'm just testing it in google chrome console(haven't gotten to downloading it yet because i rarely use it)

as for the project I'm working on its a experiment for me to learn

i don't think there any more info to give

||i should of prob joined here(like talk around and what ever, i don't know) first and then asked a question instead of joining and asking a quest straight away lol||
next pollen
#

I don't understand why you'd do something like that

vapid rune
#

uh expirment

#

anyway sovled my issue now i think i needa do !close right

#

!close

fast scrollBOT
#

You can use the /close slash command to close a help post.

mild dirgeBOT
#

ℹ️ @vapid rune this post has been closed. Feel free to reopen it if you have any further questions.

next pollen
#

It seems like you can just create a function that takes in a string

#

And from that string it just alerts

vapid rune
#

nope not for what i want

#

there is further info i haven't given but its not needed as this is separate from that but I'm doing the experiment for it if you know what i mean?

next pollen
#

eh sure

vapid rune
#

there were other errors in my code I'm fixing to but i think i know how to solve them

#

anyway i guess i should close post again(by the way i fixed it by putting the spells[i] inside a [ ] which fixed the original issue of this post