Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when i return something in init(), and then i can not call all methods #342

Closed
lornally opened this issue May 23, 2019 · 10 comments
Closed

Comments

@lornally
Copy link

lornally commented May 23, 2019

i did not certain this is a bug. this is my test code:

function teststampinit() {
  const mckpoint = stampit()
    .statics({
      keymap: {},
    })
    .init(({ lng, lat, info, ...p }) => {
      const t = {};
      t.p = p;
      t.lng = lng;
      return t;
    })
    .methods({
      info() {   //这个不能被调用. this is can not be call.
        return "ooo";
      },
    });

  const xxx = mckpoint();
  xxx.info(); //这里会提示info不是函数.
  //here will info: xxx.info is not a function
}
@koresar
Copy link
Member

koresar commented May 23, 2019

Thanks for the question!

Here is a quote from our docs

If you return anything from an initializer then it becomes the object instance.

Feel free to ask any other questions. 👍

Cheers

@koresar koresar closed this as completed May 23, 2019
@lornally
Copy link
Author

and then, how can i get a stamp when i return an object in init method.

@lornally
Copy link
Author

describe what is my target: i want a new object that has two feature:

  1. IT copy all property from another object, that object from json, so it is different every time.
  2. It is a stamp, example a mckpoint.
    and how can i get the target? thank you.

@koresar
Copy link
Member

koresar commented May 24, 2019

Hello @lornally

Looks like you need to understand the basics of stamps. I'd highly recommend going through this little article: https://medium.com/@koresar/fun-with-stamps-episode-1-stamp-basics-e0627d81efe0

It's very easy to solve your problem. :)
Do not create new objects inside the init. Think of init as a class constructor. Use this.

    .init(function({ lng, lat, info, ...p }) {
      t.p = p;
      t.lng = lng;
    })

Does that answer?

@lornally
Copy link
Author

lornally commented May 24, 2019

actually, i write code like this:

const mckpath = stampit()
  .init(function init({ start, end, list, ...p }) {
    this.start = mpoint(start);
    this.end = mpoint(end);
    this.train = train({ list });
  Object.keys(p).forEach(e => { //here i got all last property, but can we elegant more
      this[e] = p[e];
    });
  }) 

i use object.key got all property, but can we do it more elegant?
and i will read tho topic, thank you:)
and your code should use function not =>, because use 'this'

@koresar
Copy link
Member

koresar commented May 24, 2019

Thanks for the correction about =>. Indeed there should be function. Corrected now.

Instead of the forEach you should use Object.assign(this, p);

@lornally
Copy link
Author

thank you very much, and can we do it more elegant than object.assign?

@koresar
Copy link
Member

koresar commented May 26, 2019

Yep. Here is my favourite method
https://www.npmjs.com/package/@stamp/arg-over-prop

@lornally
Copy link
Author

lornally commented May 26, 2019

thank you, you are really nice:) i will learn your suggest:)
and i have see arg-over-prop, that is a good solution.

@koresar
Copy link
Member

koresar commented May 26, 2019

Thank you for the kind words mate!
Feel free to contact any time.

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants