Code Monkey home page Code Monkey logo

Comments (10)

koresar avatar koresar commented on June 26, 2024

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

from stampit.

lornally avatar lornally commented on June 26, 2024

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

from stampit.

lornally avatar lornally commented on June 26, 2024

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.

from stampit.

koresar avatar koresar commented on June 26, 2024

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?

from stampit.

lornally avatar lornally commented on June 26, 2024

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'

from stampit.

koresar avatar koresar commented on June 26, 2024

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

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

from stampit.

lornally avatar lornally commented on June 26, 2024

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

from stampit.

koresar avatar koresar commented on June 26, 2024

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

from stampit.

lornally avatar lornally commented on June 26, 2024

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

from stampit.

koresar avatar koresar commented on June 26, 2024

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

Cheers

from stampit.

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.