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

187
Views
JavaScript Question - Is there any way to assign an object to "this" keyword without using the call, apply and bind methods?

I am trying to create a polyfill for the call, apply and bind methods.

const user = {
  firstName: "Christopher",
  lastName: "Nolan",
};

const fullName = function (place, country) {
  console.log(
    `${this.firstName} ${this.lastName} is from ${place}, ${country}.`
  );
};

// Using a call method

fullName.call(user, "London", "UK");

// Re-creating a call method with the name of "_call"

Function.prototype._call = function (...args) {
  const funcObj = this;
  const params = args.slice(1);
  return (function () {
    const obj = args[0];
       
    // How can I point out the obj to "this" keyword inside a funcObj without using the bind method as I mentioned below.
         return funcObj.bind(obj, ...params)();
         })();
};

fullName._call(user, "London", "UK");
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Yes. Just use

var myObject = this;

I understand the problem you might be having. Sometimes by the time you wish to use the 'this' object the context of 'this' has changed. Some people use

var that = this;

Here's some more reading for you that goes a bit deeper.

https://dbwriteups.wordpress.com/2017/04/08/what-does-that-this-in-javascript-mean/

about 4 years ago · Juan Pablo Isaza Report

0

I'm not sure this is a great implementation, but you could take advantage of the fact that for an object method the scope is automatically set to the object upon which it was invoked.

With that in mind you could create an object using the scope as the prototype and add the original function as a method, then invoke the method.

const user = {
  firstName: "Christopher",
  lastName: "Nolan",
};

const fullName = function (place, country) {
  console.log(
    `${this.firstName} ${this.lastName} is from ${place}, ${country}.`
  );
};

Function.prototype._call = function (scope, ...args) {
  // symbol for the method name to avoid name collisions
  const symbol = Symbol();
  
  // create a new object from scope with the original function (this) as a method
  const temp = Object.create(scope, {[symbol]: { value: this }});
  
  // inside the method "this" will point to "temp" which is (effectively) "scope"
  return temp[symbol](...args);
}

fullName._call(user, 'London', 'UK');

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!