Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
Why I'm getting "Object.defineProperty called on non-object" when using a binded function as 1st parameter in Object.defineProperty

I'm trying to bind the this keyword of a function and use the Function as with object dot notation (as Functions are also objects). I figured out using the object literal 'get' to do something like: myFunction.whatever (instead of myFunction().whatever).

Code is:

let template = {name: "William"};
let aux = function(){}
callTo = aux.bind(template);

Object.defineProperty(callTo.prototype, "scream", {
    get: function() {
        console.log(this);
        return "Hi, " + this.name;
    }
});
myObj = new callTo();
myObj.scream;

This causes the: "Object.defineProperty called on non-object" error However, if I bind in each property defined with "get" it will correctly work.

let template = {name: "William"};
let callTo = function(){}

Object.defineProperty(callTo.prototype, "scream", {
    get: function() {
        console.log(this);
        return "Hi, " + this.name;
    }.bind(template)
});
myObj = new callTo();
myObj.scream;  // Outputs Hi William

The working approach works but obligues me to add a bind() at the end of each propery. So question is:

¿Why is first code failing?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda