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.

// Lets create a horse constructor because I like horses...lol
function Horse(name) {
    let speech = 'Neigh';
    this.name = name;
    this.speak = function() {
        return speech;
    }
}

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

let trigger = new Horse('Trigger');
console.log(trigger.name); // Outputs 'Trigger'
console.log(trigger.speak()); // Outputs 'Neigh'

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…

Horse.prototype.munch = function(){
    return 'munching on hay';
}
console.log(trigger.munch()); // Outputs 'munching on hay'

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…

Object.defineProperty(trigger, 'color', {value:'brown', writeable:true, enumerable:true, configurable:true});
console.log(trigger.color); // Outputs 'brown'

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

More To Explore

computer, laptop, work place-2982270.jpg
Code

Unlocking Wireless Communication: A Dive into the Bluetooth API

Wireless communication has become an integral part of our daily lives, and Bluetooth technology is at the forefront of this revolution, enabling devices to exchange data over short distances and creating a world more interconnected than ever before. At the heart of this technology lies the Bluetooth Application Programming Interface (API), a powerful tool for developers looking to harness the capabilities of Bluetooth in their applications. In this blog post, we’ll explore what the Bluetooth API is, how it works, and the possibilities it opens up for innovation in wireless communication.

lighthouse, beacon, atlantic-8578318.jpg
Code

Understanding the Beacon API: Simplifying Asynchronous Data Transfers

In today’s data-driven world, web applications often need to send data back to the server. Traditionally, this has been done using AJAX requests or similar methods. However, these techniques can come with a cost, especially when dealing with data that needs to be sent during the unload phase of a document, such as tracking and diagnostic data. This is where the Beacon API shines by allowing developers to send data to a server more reliably and efficiently.

Share This Post

small_c_popup.png

Need help?

Let's have a chat...


Login

Jump Back In!

Here at Webolution Designs, we love to learn. This includes sharing things we have learned with you. 

Register

Begin Your Learning Journey Today!

Come back inside to continue your learning journey.