JavaScript concept with a cup of coffee and a laptop

JavaScript Instances

In the last post, I briefly discussed JS constructors. In that post, I showed how to create an instance of a constructor with the new operator. Our constructor was rather ‘blah’. So let’s see how we can access constructor instance properties and methods.

The aforementioned constructor code gives us a horse constructor to be able to create multiple horse instances.

We can create any number of instances and call the properties and methods of that instance which has been “copied” from the constructor.

We can also use the prototype property built into JS to add new constructor info…

We can also use the object’s defineProperty method to create properties on the fly. The first item it expects is the object upon which to run. In this case, it is the trigger variable. The second thing it expects is the name of the property along with the value of said property…

So, as you can see, there are a myriad of ways to create a constructor and even adjust it on the fly, which may be a good or bad thing… 😉 Anyway, I hope this is helpful to you.

Happy Coding!

Clay Hess

Share this post

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest
Share on print
Share on email
Skip to content