Call method in class from another method in same c

2019-05-07 03:23发布

I am trying to call the method clean from getUser, but it returns undefined. If I call u.test(), it works perfectly.

How can I solve this issue?

class User
    constructor: () ->
        @db = # connect to db...

    clean: (user, callback) ->
        delete user.password
        callback user


   getUser: (id) ->
       @db.get id, (err, user) ->
            @clean user, (u) -> console.log u

   test: () ->
           @clean {name: "test", password: "hello"}, (u) ->
                console.log u

u = new User
u.getUser()

1条回答
叛逆
2楼-- · 2019-05-07 03:39

You want => for the inner function.

In your inner function, with ->, it's a normal function bound to undefined by default. With =>, you bind it to the this value of the function instantiation context.

查看更多
登录 后发表回答