Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
Executing a function that runs directly by adding it at the end of the variable

Most of the function had to run by adding a parameter but in this case I just want to work like this:

let value = "test";

value.funcTest();

function funcTest(){

return "value replaced" + value;

}

instead of

let value = "test";

value  = funcTest(value);

function funcTest(x){

return "value replaced" + x;

}

is there a way to pull this off?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Given

let value = "test";
value.funcTest();

It's only possible if you add a method to String.prototype - which is a very bad idea and shouldn't be used.

String.prototype.funcTest = function() {
  return "value replaced" + this;
}
let value = "test";
console.log(value.funcTest());

If you want to tie the string and a function together without using a function call of a separate identifier, a better approach would be to use an object instead, and put both in the object.

const obj = {
  value: "test",
  funcTest() {
    return "value replaced" + this.value;
  }
}
console.log(obj.funcTest());

about 4 years ago · Juan Pablo Isaza Report

0

I'm not entirely sure what you're trying to do, but maybe using an object would work:

let variable =
{
  value: 'test',
  funcTest: function()
  {
    return 'value is ' + this.value;
  }
};

If you don't want to have to add the parentheses you could also use a getter:

let variable=
{
  value: 'test',
  get funcTest()
  {
    return 'value is ' + this.value;
  }
};

This way you could just call variable.funcTest and it would do the same thing.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!