How to quick to get everything from destructuring assignment? - javascript

For example, I have a object with server functions.
let funs = {
fun1: () => { console.log('fun1') },
fun2: () => { console.log('fun2') },
fun3: () => { console.log('fun3') },
};
then I want to have quick way to use this function set.
class SampleClass {
sampleFun() {
{...funs} // anything similar to it?
fun1();
}
}

It's not possible.
I close alternative is calling that function, using the function call with funs as lexical context.
let funs = {
fun1: () => { console.log('fun1') },
fun2: () => { console.log('fun2') },
fun3: () => { console.log('fun3') },
};
class SampleClass {
sampleFun() {
this.fun1();
this.fun2();
}
}
new SampleClass().sampleFun.call(funs);

You could pull the functions into the namespace of your instance with Object.assign(), then you could call with this.fun1(), etc...
let funs = {
fun1: () => { console.log('fun1') },
fun2: () => { console.log('fun2') },
fun3: () => { console.log('fun3') },
};
class SampleClass {
constructor(funs) {
Object.assign(this, funs)
}
sampleFun() {
this.fun1()
this.fun2()
this.fun3()
}
}
let s = new SampleClass(funs)
s.sampleFun()

There is no way to do that. In other words you are asking for Dynamic variable names which are not possible without using eval(). Here is your code the names of function are in series you can call all using loop.
let funs = {
fun1: () => { console.log('fun1') },
fun2: () => { console.log('fun2') },
fun3: () => { console.log('fun3') },
};
class SampleClass {
constructor(funs) {
Object.assign(this, funs)
}
sampleFun() {
for(let i = 1;i<=3;i++){
this[`fun${i}`]();
}
}
}
let s = new SampleClass(funs)
s.sampleFun()

I think all answers are correct, but I'm actually looking for shortcut form; like what #CertainPerformance said, there is no way to have shortcut expression

Related

Javascript: update an object from its method without this keyword

I am working with a library which implement a class Graph that have a method Graph.registerNode()
The methode has the following signature.
Graph.registerNode('node-name', {
object: {
options: {
key: "value"
},
updateOption: () => {
this.options = { key: "other value" }
}
})
Is it possible to update the options key from updateOptions without using the this keyword?
const options = {
key: "value"
};
Graph.registerNode('node-name', {
object: {
options,
}
updateOption: () => {
options.key = "other value";
}
})
How about this?
I was able to solve this via a factory function
const factoryFunction = () => {
let options: {
key: "value"
}
return Object.freeze({
updateOption: () => {
options = {
key: 'updated value',
}
},
printOption: () => {
console.log(options)
}
})
}
const config = factoryFunction()
Graph.registerNode('node-name', config)

is there a way to use strings with some object's property in javascript? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed last year.
it's hard to explain, bur lets go on with a example
const exampleProperty = {
log1: () => {
console.log('log1')
},
log2: () => {
console.log('log2')
},
log3: () => {
console.log('log3')
},
log4: () => {
console.log('log4')
}
}
for (let i = 0; i < 4; i++) {
// now here I want to run all the functions in the example Property
// I imagined it something like
exampleProperty.log${i}
// but this is invalid syntax
}
There might be a real easy answer to this but as I am new i really can't imagine what to do
If you wanted to enumerate every method
const exampleProperty = {
log1: () => {
console.log('log1')
},
log2: () => {
console.log('log2')
},
log3: () => {
console.log('log3')
},
log4: () => {
console.log('log4')
}
}
Object.values(exampleProperty).forEach(p => p())
Working example, using backticks
const exampleProperty = {
log1: () => {
console.log('log1')
},
log2: () => {
console.log('log2')
},
log3: () => {
console.log('log3')
},
log4: () => {
console.log('log4')
}
}
for (let i = 1; i <= 4; i++) {
// now here I want to run all the functions in the example Property
// I imagined it something like
exampleProperty[`log${i}`]();
// but this is invalid syntax
}

Why at others versions writing code function is and is't a constructor?

I have a object with function
{
ready(_callback) {
_callback();
},
Map() {
this.geoObjects = () => {
return {
add() {},
get() {
return {
removeAll() {},
add() {},
};
},
}
};
this.setCenter = () => {};
this.setZoom = () => {};
},
},
an then I write
Map: function () { // it is a constructor
Map() { // it is not a constructor
Map: () => { //it is not a constructor
Why? Help pls. And how to write this object with a constructor function on ES6 format?

How to add a function to Object's named property with correct context in javascript

I have a working piece of code as below:
let pageParams = {
data: { todos: [], desc: '' }
}
pageParams.onLoad = function () {
//I am trying to encapsulate this to a standalone function and
// make it generic, instead of hard coding the 'this.addTodo=XXX'
const evProducer = {
start: listener => {
//Here, I am adding a named property function
this.addTodo = ev => {
listener.next(ev.detail.value)
}
},
stop: ()=>{}
}
const input$ = xs.create(evProducer)
input$.compose(debounce(400)).subscribe({
next: val => console.log(val)
})
}
The code works and now I am going to do some refactor work, i.e. move the logic out of this onLoad function. So I move the logic to another module
let xsCreator = {}
xsCreator.fromEvent = function(handler){
const evProducer = {
start: listener => {
handler = ev => listener.next(ev.detail.value)
},
stop: () => {}
}
return xs.create(evProducer)
}
And in the previous onLoad function becomes the following:
pageParams.onLoad = function () {
xs.fromEvent(this.addTodo).subscribe(blablabla)
}
but it does not work. I guess I might use apply/call/bind to make this work, but don't know how to. Anyone can help? Thanks in advance
I've found the solution, I should use Object.defineProperty to add a named property for object.
xsCreator.fromInputEvent = (srcObj, propertyName) => {
const evProducer = {
start: (listener) => {
Object.defineProperty(
srcObj,
propertyName,
{value: ev => listener.next(ev.detail.value)})
},
stop: () => {}
}
return xs.create(evProducer)
}

javascript calling parent method from child

I have the following object:
var party =
{
food:
{
serve: function () {
// I want to call turnOff method from here
}
cleanUp: function () {
}
}
music:
{
turnOff: function () {
}
}
}
So as the comment points out, I want to call the turnOff method from the music object, how can I do this? this refers to the food object but I need to access the music object...
var party =
{
food:
{
serve: function () {
party.music.turnOff();
},
cleanUp: function () {
}
},
music:
{
turnOff: function () {
}
}
}
Use a constructor instead of a literal with a variable referencing the parent object
var party = new (function()
{
var self = this;
this.food =
{
serve: function () {
self.music.turnoff();
},
cleanUp: function () {
}
}
this.music =
{
turnOff: function () {
}
}
})();
Call it as party.music.turnOff().
FYI, your above code block isn't valid. You're missing some commas - after the serve and food closing braces.

Categories