Git Product home page Git Product logo

Comments (2)

albyrock87 avatar albyrock87 commented on May 30, 2024 1

This issue has been fixed by this commit.

from ngrx-actions.

albyrock87 avatar albyrock87 commented on May 30, 2024
function Selector(fn: () => string) {
  return function (target, propertyKey: string, descriptor: PropertyDescriptor) {
    console.log(fn.bind(target)());
  }
}

class Foo {
  @Selector(() => {
    return this.fn();
  })
  bar: string;

  fn() {
    return "hello";
  }
}

transpiles to:

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var _this = this;
function Selector(fn) {
    return function (target, propertyKey, descriptor) {
        console.log(fn.bind(target)());
    };
}
var Foo = /** @class */ (function () {
    function Foo() {
    }
    Foo.prototype.fn = function () {
        return "hello";
    };
    __decorate([
        Selector(function () {
            return _this.fn();
        })
    ], Foo.prototype, "bar", void 0);
    return Foo;
}());

You can notice that this becomes a _this that is not the object reference.
That's because the decorator is invoked at "class definition" time.

If you want to achieve your purpose you should do something like this:

function Selector(fn: () => string) {
  return function (target, propertyKey: string, descriptor: PropertyDescriptor) {
    console.log(fn.bind(target)());
  }
}

class Foo {
  @Selector(function(){
    return this.fn();
  })
  bar: string;

  fn() {
    return "hello";
  }
}

Note the normal function instead of the => arrow function.

from ngrx-actions.

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.