I have an object that looks like below. Basically it contains properties that call a method. What I would like to be able to do is, instead of having DEVELOPER twice (Once for the property name and once for the parameter value), I'd like to get the current property of what was called in order to get this.
Also I don't want to pass in 'DEVELOPER' as a parameter in the initial call because I want intellisense to pick it up.
return {
DEVELOPER: function ()
{
return getEmailRecipients("DEVELOPER")
}
}
//it get's called like this.
emailGroups.DEVELOPER();
Essentially I'd like to do something like
return {
DEVELOPER: function ()
{
return getEmailRecipients({this.currentPropName}) //Which would equal DEVELOPER.
}
}
If there is a better way to do this, I am all ears.
Thanks in advance!
As far as I know, this is not possible, or at least it's not built-in.
You can use the function name as defined in arguments.callee like this...
arguments.callee.name
But the name property of functions is not well supported.
You might try something like this...
this.toSource().match(/\{(.*?):/)[1].trim ()
But that only works for one item in an object, and only the first item.
You might also try defining the object you return as a variable, and loop over that object giving every item a new property that contains the item name.
Like so...
// Object to return to emailGroups
var data = {
DEVELOPER: function ()
{
return getEmailRecipients (arguments.callee.givenName)
}
}
// Give each object item a new property containing the name
for (var name in data) {
if (data.hasOwnProperty (name) === true) {
data[name].givenName = name;
}
}
// Return data variable as emailGroups
return data;
Related
I have a utils/constant.js file with:
// Key of total elements in remote collection
export const TOTAL_ELEMENTS = "totalElements";
I need to access to totalElements via the constant.
import { TOTAL_ELEMENTS } from "./constants.js";
[...]
let data = {
content: "foo",
totalElements: 54
};
if(TOTAL_ELEMENTS in data) {
// pseudocode, of course it doesn't work.
// of course in my case need to return 54
const { TOTAL_ELEMENTS } = data;
return TOTAL_ELEMENTS;
}
Edit:
As #pilchard mentioned, using Object.prototype.hasOwnProperty is a better way of doing this in case the value is falsy:
if (data.hasOwnProperty(TOTAL_ELEMENTS)) {
return data[TOTAL_ELEMENTS]
}
Original answer:
While the answer #jsN00b provided works and is closer to OP's #sineverba code, there's an issue here since the in operator checks for the specified property in both the specified object AND its prototype chain.
This means that, for example, if datas prototype is Object.prototype, something like 'toString' in data would work as well.
For that reason, you could use something like the following to only check for the 'totalElements' key in the object itself, and avoid destructuring:
if (data[TOTAL_ELEMENTS]) {
return data[TOTAL_ELEMENTS]
}
The desired objective is:
use the constant TOTAL_ELEMENTS (& not directly the prop-name)
check if data has the corresponding prop
if found, then return the value of the prop
The below code-sample may be one solution to achieve the desired objective:
if (TOTAL_ELEMENTS in data) return data[TOTAL_ELEMENTS];
NOTE
The above does not de-structure the data. It access the corresponding prop directly without the need to destructure.
I am trying to write a universal function that can log some useful data in several different objects, but I don't want to have to pass a parameter that is unique to each object. Ideally, the initial name of the object, like LaunchRequestHandler would be a perfectly sufficient bit of data to use. How can I access this object's name?
const LaunchRequestHandler = {
canHandle(handlerInput) {
//I want to be able to know the name of the outer parent here.
//In this case, I would expect to retrieve 'LaunchRequestHandler'
logCanHandle(handlerInput);
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
}
I don't really understand what are you trying to do but maybe you can do something with ES6 classes, like so:
class LaunchRequestHandler {
canHandle() {
console.log(this.constructor.name);
}
};
const x = new LaunchRequestHandler();
x.canHandle(); // outputs 'LaunchRequestHandler'
I have an array of objects, say memberToChange.checkboxes: ICheckbox[] like this:
Now, I have a variable, say internalNumber: string which has the value "3419". I want to get the object from the array of objects where the internalNumber matches the label property. Ultimately, I want to set the value attribute of that object to true.
My code is:
let checkboxes = _.find(scope.selectedMembers, (member: IMember) => member.member.uuid === memberId).checkboxes; //gives me array of checkboxes.
let checkboxToChange = _.find(memberToChange.checkboxes, function(checkbox: ICheckbox){
return (checkbox.label === internalNumber);
}); //gives me null, moreover, even a console.log inside the second find doesn't print. I'm thinking the two consecutive _.find statements are messing something up, not sure what.
For reference, this is my ICheckbox interface:
export interface ICheckbox {
label: string;
sublabel?: string;
value: boolean;
numberUuid: string;
}
I would expect that for internalNumber 3419, it should return me the second object from the array. But it returns undefined. I'm not sure what's going on here.
If there is a better way to find and set the value to true in one go only, I'd be happy to know that as well.
Any help is appreciated. Thank you.
Update:
After someone suggested using filter method of javascript, I tried this: (my scope is assigned to this)
scope.selectedMembers.filter(function(member) {
if (member.member.uuid === memberId) {
scope.memberCheckboxes = [];
console.log('found member'); //prints
scope.memberCheckboxes = member.checkboxes;
console.log(scope.memberCheckboxes); // print correctly, with checkboxes of the member
scope.memberCheckboxes.filter(function(checkbox) {
console.log('inside checkbox function'); //control doesnt even come here
if (checkbox.label === intNum) {
console.log('found checkbox'); // control doesnt come here
}
});
}
});
Here, I don't understand why the first console.log inside scope.memberCheckboxes.filter doesn't print? Am I missing something obvious here?
By some reason your memberToChange.checkboxes (or member.checkboxes in your updated question) have no elements.
It is the only explanation why it does not work since your code is otherwise correct. The fact that console.log('inside checkbox function') does not print confirms that.
I am trying to run some JavaScript, but it is not working.
I have an object with two properties that are also objects.
var people = {
me: {
name: "Hello"
},
molly: {
name: "Molly"
}
};
And I am trying to make a function that uses a for/in statement and an if statement to list the properties of people.
var search = function (x) {
for (var a in people) {
if (people.a.name === x) {
return people.a;
}
}
};
So the function loops through the properties of people and assigns them to the variable a. Therefore people.a will be equal to a property of people. Then the function returns the property (people.a).
So if I type in me as parameter x, will the function should return the properties for the me object? I put this code in jsLint and jsHint and it passed, but I decided to remove the corrections because they were useless.
I then want to print the object properties in the browser:
var print = search("me");
document.getElementById("p").innerHTML(print);
I have this linked to an html document, with a tag id "p". I have tested javascript in the html document already, so I know that the javascript document is linked properly.
But the code will not work. Does anyone have any suggestions?
I have it working now thanks to the answers. But I thought that it would only print "Hello" to the screen, not { name: "Hello"}.
You need to use people[a], not people.a. The former looks for a property with the name of the value stored in a; the latter looks for a property literally named "a", which of course doesn't exist.
for (var a in people) {
if (people[a].name === x) {
return people[a];
}
}
Fiddle here.
Also, I think you meant search("Hello"), right? If not, then it would just be var search = function(x) { return people[x]; }.
people.a.name
you need to use the bracket operator if you want to access an item by name. Using people.a is literally searching for a member named 'a' instead of a member with the same name as the value of a.
Try:
people[a].name
instead.
4 errors in your code:
replace people.a with people[a]
replace innerHTML() with innerHTML
set HTML like this: document.getElementById("p").innerHTML = print.name;
As in a previous answer, search by name
Code: http://jsfiddle.net/nabil_kadimi/vVSPG/
Is it possible to find the name of an anonymous function?
e.g. trying to find a way to alert either anonyFu or findMe in this code http://jsfiddle.net/L5F5N/1/
function namedFu(){
alert(arguments.callee);
alert(arguments.callee.name);
alert(arguments.callee.caller);
alert(arguments.caller);
alert(arguments.name);
}
var anonyFu = function() {
alert(arguments.callee);
alert(arguments.callee.name);
alert(arguments.callee.caller);
alert(arguments.caller);
alert(arguments.name);
}
var findMe= function(){
namedFu();
anonyFu();
}
findMe();
This is for some internal testing, so it doesn't need to be cross-browser. In fact, I'd be happy even if I had to install a plugin.
You can identify any property of a function from inside it, programmatically, even an unnamed anonymous function, by using arguments.callee. So you can identify the function with this simple trick:
Whenever you're making a function, assign it some property that you can use to identify it later.
For example, always make a property called id:
var fubar = function() {
this.id = "fubar";
//the stuff the function normally does, here
console.log(arguments.callee.id);
}
arguments.callee is the function, itself, so any property of that function can be accessed like id above, even one you assign yourself.
Callee is officially deprecated, but still works in almost all browsers, and there are certain circumstances in which there is still no substitute. You just can't use it in "strict mode".
You can alternatively, of course, name the anonymous function, like:
var fubar = function foobar() {
//the stuff the function normally does, here
console.log(arguments.callee.name);
}
But that's less elegant, obviously, since you can't (in this case) name it fubar in both spots; I had to make the actual name foobar.
If all of your functions have comments describing them, you can even grab that, like this:
var fubar = function() {
/*
fubar is effed up beyond all recognition
this returns some value or other that is described here
*/
//the stuff the function normally does, here
console.log(arguments.callee.toString().substr(0, 128);
}
Note that you can also use argument.callee.caller to access the function that called the current function. This lets you access the name (or properties, like id or the comment in the text) of the function from outside of it.
The reason you would do this is that you want to find out what called the function in question. This is a likely reason for you to be wanting to find this info programmatically, in the first place.
So if one of the fubar() examples above called this following function:
var kludge = function() {
console.log(arguments.callee.caller.id); // return "fubar" with the first version above
console.log(arguments.callee.caller.name); // return "foobar" in the second version above
console.log(arguments.callee.caller.toString().substr(0, 128);
/* that last one would return the first 128 characters in the third example,
which would happen to include the name in the comment.
Obviously, this is to be used only in a desperate case,
as it doesn't give you a concise value you can count on using)
*/
}
Doubt it's possible the way you've got it. For starters, if you added a line
var referenceFu = anonyFu;
which of those names would you expect to be able to log? They're both just references.
However – assuming you have the ability to change the code – this is valid javascript:
var anonyFu = function notActuallyAnonymous() {
console.log(arguments.callee.name);
}
which would log "notActuallyAnonymous". So you could just add names to all the anonymous functions you're interested in checking, without breaking your code.
Not sure that's helpful, but it's all I got.
I will add that if you know in which object that function is then you can add code - to that object or generally to objects prototype - that will get a key name basing on value.
Object.prototype.getKeyByValue = function( value ) {
for( var prop in this ) {
if( this.hasOwnProperty( prop ) ) {
if( this[ prop ] === value )
return prop;
}
}
}
And then you can use
THAT.getKeyByValue(arguments.callee.caller);
Used this approach once for debugging with performance testing involved in project where most of functions are in one object.
Didn't want to name all functions nor double names in code by any other mean, needed to calculate time of each function running - so did this plus pushing times on stack on function start and popping on end.
Why? To add very little code to each function and same for each of them to make measurements and calls list on console. It's temporary ofc.
THAT._TT = [];
THAT._TS = function () {
THAT._TT.push(performance.now());
}
THAT._TE = function () {
var tt = performance.now() - THAT._TT.pop();
var txt = THAT.getKeyByValue(arguments.callee.caller);
console.log('['+tt+'] -> '+txt);
};
THAT.some_function = function (x,y,z) {
THAT._TS();
// ... normal function job
THAT._TE();
}
THAT.some_other_function = function (a,b,c) {
THAT._TS();
// ... normal function job
THAT._TE();
}
Not very useful but maybe it will help someone with similar problem in similar circumstances.
arguments.callee it's deprecated, as MDN states:
You should avoid using arguments.callee() and just give every function
(expression) a name.
In other words:
[1,2,3].forEach(function foo() {
// you can call `foo` here for recursion
})
If what you want is to have a name for an anonymous function assigned to a variable, let's say you're debugging your code and you want to track the name of this function, then you can just name it twice, this is a common pattern:
var foo = function foo() { ... }
Except the evaling case specified in the MDN docs, I can't think of any other case where you'd want to use arguments.callee.
No. By definition, an anonymous function has no name. Yet, if you wanted to ask for function expressions: Yes, you can name them.
And no, it is not possible to get the name of a variable (which references the function) during runtime.