Closed lornally closed 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
and then, how can i get a stamp when i return an object in init method.
describe what is my target: i want a new object that has two feature:
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?
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'
Thanks for the correction about =>
. Indeed there should be function
. Corrected now.
Instead of the forEach
you should use Object.assign(this, p);
thank you very much, and can we do it more elegant than object.assign?
Yep. Here is my favourite method https://www.npmjs.com/package/@stamp/arg-over-prop
thank you, you are really nice:) i will learn your suggest:) and i have see arg-over-prop, that is a good solution.
Thank you for the kind words mate! Feel free to contact any time.
Cheers
i did not certain this is a bug. this is my test code: