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

176
Views
Pregunta de JavaScript: ¿hay alguna forma de asignar un objeto a "esta" palabra clave sin utilizar los métodos de llamada, aplicación y vinculación?

Estoy tratando de crear un polyfill para los métodos de llamada, aplicación y vinculación.

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

// Usando un método de llamada

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

// Volver a crear un método de llamada con el nombre de "_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

Sí. Solo usa

 var myObject = this;

Entiendo el problema que puedes tener. A veces, cuando desea utilizar el objeto 'esto', el contexto de 'esto' ha cambiado. Algunas personas usan

 var that = this;

Aquí hay más lectura para usted que va un poco más profundo.

https://dbwriteups.wordpress.com/2017/04/08/que-eso-significa-en-javascript/

about 4 years ago · Juan Pablo Isaza Report

0

No estoy seguro de que sea una gran implementación, pero podría aprovechar el hecho de que, para un método de objeto, el alcance se establece automáticamente en el objeto sobre el que se invocó.

Con eso en mente, podría crear un objeto usando el alcance como prototipo y agregar la función original como método, luego invocar el método.

 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!