stampit-org / stampit

OOP is better with stamps: Composable object factories.
https://stampit.js.org
MIT License
3.02k stars 102 forks source link

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

Closed lornally closed 5 years ago

lornally commented 5 years ago

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 commented 5 years ago

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

lornally commented 5 years ago

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

lornally commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

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

lornally commented 5 years ago

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

koresar commented 5 years ago

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

lornally commented 5 years ago

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

koresar commented 5 years ago

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

Cheers