Attaching components to instantiated objects

Very basic question about instantiated objects… how do you attach components (like scripts) to instantiated objects, since they do not yet exist in a scene?

Building on top of the previous answer:

  1. Option 1: If you know ahead of time what are the components you need for an object, create a prefab of this object and then instantiate the prefab. You can always edit the prefab and add components to it by selecting it in the project hierarchy.
  2. Option 2: If you do not know, and must add components on the fly programmaticly, you can use AddComponent