Giter Club home page Giter Club logo

Comments (2)

dortamur avatar dortamur commented on August 12, 2024

I've been reading up more on how "this" is handled in JS, and have a little better understanding of some of the issues now.

Building on my simplified example, these two techniques seem to work:

Binding "this" to a copy of the prototype function in the constructor:

function User(name) {
   this.initial_name = name;
   this.default_name = 'Barry';
   this.custom_name = null;
   this.name = this.name.bind(this);
}

User.prototype.name = function() {
   return this.custom_name || this.initial_name || this.default_name;
}

Creating and binding the method function within the constructor:

function User(name) {
   this.initial_name = name;
   this.default_name = 'Barry';
   this.custom_name = null;
   this.name = function() {
      return this.custom_name || this.initial_name || this.default_name;
   }.bind(this);
}

If I understand correctly, the second technique is less efficient (defines the method function for every constructor).

Is the first technique correct, and is it the best way to handle this situation, or are there better ways?

Thanks.

from hbs.

defunctzombie avatar defunctzombie commented on August 12, 2024

The bind approach will work but I think your first approach should work as well. It might be that handlebars has a bug when calling functions and not specifying the context properly. We are several versions behind the latest handlebars which is 2.0.0 now. 2.0 was in beta for a long time but seems to have now landed. Maybe it is time to upgrade.

from hbs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.