Detecting property of caller function

How do I detect the properties of the caller function? I want the called function to return different values to the caller depending on the property called with the function.

For example, if the caller function has the property x with the value test , I want the original function to return "y; value:" + valueOfX ( "y; value:test" ). Likewise if the caller function has property a with value of test2 , I want the original function to return "b; ValueOfA:" + valueOfA ( "b; ValueOfA:test2" ). If the caller function has no property, I want it to return n .

I’ve tried searching it up but only got answers on detecting the caller function, not the properties.

Thanks.

@GeneralX Can you provide an example call to “caller function”? I am not sure what you mean by property x? Do you mean function argument instead of property? An example call to the function might help us understand what you are trying to do. If you have the actual function, post that too.

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

By caller function I mean something like this:

info("joe").age("27")

or

info("joe").age = "27"

(both should work) where the original function is something like

function info(name){[code here]},

the property is

.age,

and the property value is

"27".

You have not shown the actual code for the function named info. Can you show the code of what info would do/return?

I ask because neither of the following would be valid function calls.

info("joe").age("27")

or

info("joe").age = "27"

I want it to return different things depending on the property or whatever the .age part is called.

I may be misunderstanding what you are asking. Can you please show an example of the full code of your info function? I am not 100% sure you are talking about a function. I believe you might be talking about an object, since you mention “property”. Objects have properties.

I don’t have a fully developed function yet but it would go something like this:

function info(name){
age: function(age){
name = age //set age as name
}
}

Or for example

function myFunction(element){
html: function(html){
return document.querySelector(element).innerHTML
}
}

so myFunction(“body”).html(“hi”) or myFunction(“body”).html = “hi” will change the bodies inner HTML to hi.

This is not valid function syntax, so I am not sure what you are trying to do here.

I’m looking for a valid way to do this.

I think you might first benefit from a basic JavaScript course, as it seems you are not understanding what valid function syntax looks like. I suggest starting with our Basic JavaScript challenes and then progress through the Basic Data Structures, Object Oriented Programming and Functional Programming challenges. I believe once you go through these challenges, you will have a better idea of what questions to ask, if you do not end up answering your own questions.

Someone else might be able to help you if they ask you enough questions, but my bet is you will still need to build of your basic knowledge a bit more for it all to sink in.

Although is not completely clear what you want to accomplish, or more importantly why, this question intrigues me. The functionality you are trying to obtain reminds me of jQuery library, I think you should check that out.

What you describe is a function that returns an object in wich you can call methods and set properties. As far as I know there is no way to have an object with a property and a method with the same name, so you wont be able to do both: function().html = 'something' and function().html('something').

Nevertheless similar behavior can be obtained with minimal changes by calling the method differently. For example prefixing the method with ‘set’, like: function().setHtml('something'). Another thing that works is capitalization, as having a method named Html is different from a property html.

I’ve made an example query function that receives a selector parameter and allows to get and set the innerHtml and style of the element that corresponds to the given selector. The function returns an object where you can call setHtml() and setStyle(), also has html and style properties.

An example use of the function would be:

query('body')
.setHtml('Hi')
.setStyle({ "text-align": "center" });

As you can see the methods are chainable, because each method returns the same object so you can keep invoking methods. If you dont need to chain more methods or just want to get the value of the properties you can use for example:

query('body').html = 'Hello';  //  to set
const someVar = query('body').html;  //  to get

This is just an example, to be useful the query function would have to be extended with methods to get and set other needed properties of html elements. If this is what you need then the answer is to look at jQuery or other libraries for DOM manipulation like React.

Finally, i’ll leave here the code of the query function in case the example’s link breaks some day.

query function
function query(selector) {
  return {
    element: document.querySelector(selector),
    get html() {
      return this.element.innerHTML;
    },
    set html(value) {
      this.element.innerHTML = value;
    },
    setHtml(value) {
      this.html = value;
      return this;
    },
    get style() {
      return this.element.style;
    },
    set style(properties) {
      for (const [key, value] of Object.entries(properties)) {
        this.element.style[key] = value;
      }
    },
    setStyle(value) {
      this.style = value;
      return this;
    }
  };
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.