Differentiate between object property call and get [duplicate] - javascript

This question already has answers here:
Add a return type of string when a function is accessed like a variable
(2 answers)
Passing Argument to JavaScript Object Getter
(3 answers)
JavaScript - Variable/object as string and function at the same time
(1 answer)
Closed 23 days ago.
Assume we have the following object
const foo = {
bar: "hello "
}
Is it possible to achieve the following result:
console.log(foo.bar) // logs "hello "
console.log(foo.bar("world")) // logs "hello world"
I want to be able to get or call the same property and apply a function if called
I have tried using Proxy https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy with apply() handler, however I was not able to find a working solution, every attempt ends at This expression is not callable. Type 'String' has no call signatures.

Related

What type of operation occurs with the syntax object["key String"] JavaScript [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
How does JavaScript VM implements Object property access? Is it Hashtable?
(4 answers)
Closed last year.
I would like to know more about the internal workings of JavaScript to better understand what is happening.
With the following object
let test = {"A": 10,"D" : 6, "PP" : 777};
console.log (test["PP"]): // 777
console.log (test.PP): // 777
How does JavaScript find my value with potential N element in the object?
My question is about the inner workings.
Imagine an object with 100,000 properties how will it find the key "PP" what job will it do? How does JS work to give us the value 777?

JavaScript puzzling output [duplicate]

This question already has answers here:
Object Using name of prototype function name instead of its property
(2 answers)
When I assign prototype to function I get undesired output
(4 answers)
Does Javascript writable descriptor prevent changes on instances?
(3 answers)
Closed 3 years ago.
Consider the code below. The d1 object so created, does not have name property but why ? However, if I remove the setting of prototype as function object, things works fine. please note that below code is deliberately written like that to test how JS reacts !
function Dog(name){
this.name = name;
}
// Notice that I am putting function object as prototype
Dog.prototype = function(){}
var d1 = new Dog("happy");
console.log(d1.name); //gives empty string
console.dir(d1); // `d1` does not have name property
console.log(d1 instanceof Dog);// returns true ??

How to make string to object or array format [duplicate]

This question already has answers here:
Access a nested property with a string [duplicate]
(5 answers)
Get global variable dynamically by name string in JavaScript
(6 answers)
Closed 4 years ago.
I have to create a string in format like below
"currentState[0]['children'][1]"
But I need to execute it later just like below
currentState[0]['children'][1]
I have elements and childrens on currentState. But while looping I have to create a string. But later I need to execute as array.
I have tried almost all array methods. Array.call, bind etc. And string methods as well. Could not get the output
How can I make it
Please be more specific with your question but from my understanding, you can use javascript's eval() function to execute a string as javascript, so when you need to execute it, just run eval("currentState[0]['children'][1]").
The alternative to the eval() would be
function evalFn(obj) {
return Function('"use strict";return (' + obj + ')')();
}
evalFn("currentState[0]['children'][1]")
refer to: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval for a more in-depth explanation.

What is this notation in JS, can someone please explain [duplicate]

This question already has answers here:
Javascript object literal: what exactly is {a, b, c}?
(3 answers)
Closed 4 years ago.
I came across this function, called generateMessage which takes 2 parameters and returns and object. The function is as follows:
var generateMessage = (from, text)=>{
return {
from,
text,
createdAt: new Date().getTime()
}
};
module.exports = {generateMessage};
This does NOT throw any errors, and attaches 3 properties to the returned object: '.from' , '.text' and '.createdAt', I am confused about the '.from' and '.text' properties.
My question is why don't we write from: from , text:text, in this way the returned object will have a proto property of .from and .text, which will have their values as the from and text from the parameters.
Why does just writing from and text for the returned object work in this case?
That's ECMAScript's 'shorthand''s property and notation:
http://es6-features.org/#PropertyShorthand
http://es6-features.org/#ObjectMatchingShorthandNotation
It's as the name suggests, a shorthand method of object definition.

How to destructure an object into parameter names [duplicate]

This question already has answers here:
Is there a way to provide named parameters in a function call in JavaScript?
(12 answers)
How to get function parameter names/values dynamically?
(34 answers)
Closed 4 years ago.
I have many functions defined elsewhere that look like this
export function foo(bar1, bar2, bar3, ...) {
// does stuff
}
In the module I am working on I am passed both the function above and an object containing the parameters like this:
// params = {
// bar1: bar1Value,
// bar2: bar2Value,
// bar3: bar3Value,
// ...
// }
function myFunc(foo, params) {
}
I need to call func with the params in the correct order. It is an object not an array so I can't rely on the property order, I need to match up the params to the signature of the function. Is this possible?
Edit to add: There are hundreds of functions that can be passed in, I am trying to avoid refactoring them all, but it seems that it is impossible to look up the parameter names directly. However I've found this which shows it can be done by breaking down the function as a string.
You can access the properties of an object (like "params") either like
params.bar1 or like params["bar1"] and you can get the names of the properties by using Object.keys(params), if thats what your asking for.
See also https://www.w3schools.com/js/js_object_properties.asp and https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Categories

Resources