Prototypical Javascript setter function approach

Currently diving into Javascript. Encountering an issue when attempting to utilize setters in prototyping.

Error Message: TypeError: cir1.radiusToCircle is not a function

var Circle = function(radius){
    this._radius = radius;
}

//prototype

Circle.prototype ={
    set radiusToCircle(rad) { this._radius = rad; },
    get radiusFromCircle() { return this._radius;},

    get area() { return (Math.PI * (this._radius * this._radius));}
};

var cir1 = new Circle(5);

cir1.radiusToCircle(14);

In search of the missing link here...

Appreciate any guidance provided.

Answer №1

In order to utilize a setter, you must assign the property directly rather than passing it as a function argument. Check out the Setter MDN Documentation for more information.

circle1.setRadius = 14;

Answer №2

It's possible that your approach is effective, but without knowing the specific set/get commands you are utilizing and whether overloading the function is advisable, it's hard to say for sure.

Based on the documentation, it seems like the set/get functions will attach a callback that executes unrelated code when directly assigning the member variable. If this aligns with your intention, there may be no need for a prototype function.

var Circle = function(radius){
    this._radius = radius;
}

Circle.prototype.getRadiusToCircle = function() {
    return this._radius;
}

Circle.prototype.setRadiusToCircle = function(rad) {
    this._radius = rad;
}

Circle.prototype.overloadRadiusToCircle = function(rad) {
    if(rad != null) {
        this._radius = rad;
    } else {
        return rad;
    }
}

Circle.prototype.area = function() {
    return (Math.PI * (this._radius * this._radius));
}

For instance

var Circle = function(radius){
    this._radius = radius;
    this._area = Math.PI * (radius * radius);
}

Circle.prototype = {
    set _radius(radius) {
        this._area = Math.PI * (radius * radius);
    }   
}

Circle.prototype.getArea = function() {
    return _area;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Show the form when the button is clicked

I want to create an application that allows users to click on a button in the top right corner (check image) to hide one div (topics) and show another div (a form for adding a new topic). Here is what it looks like: https://i.stack.imgur.com/YeRTw.png ...

Exploring Angular2: Understanding how to retrieve an object with an unknown key

I am trying to showcase a specific value from different objects within a template. The path of the desired value depends on the object being referenced. For example: let obj = { "a": "a", "b": { "1": "1", "2": "READ ME" } } let ...

What are the best methods to prevent infinity printing?

While attempting to create a clock that displays time in real-time after adding a time zone, I encountered an issue where the time was being printed multiple times. My objective is to have it printed once and dynamically change according to the different t ...

Creating a never-ending scroll feature on a static page in Next.js

I am in the process of creating a portfolio using Next.js and have a large number of projects on the page. I would like to implement a feature where images start loading only when they enter the current viewport. This functionality works well with the defa ...

Utilize AngularJS to loop through a list with ng-repeat

Seeking guidance as an Angular newbie. The ng-repeat in question is formatted as: ng-repeat="f in drillDownList['D' + d.merchMetrics.DEPT_NBR + 'CG' + d.merchMetrics.CATG_GRP_NBR + 'C' + d.merchMetrics.DEPT_CATG_NBR] M ...

Signing up for event using react leaflet

I've been working on integrating React-Leaflet into my Create React App. I've successfully overlaid GeoJSON data on a base map, but I'm struggling to register clicks on this layer. While troubleshooting this problem, I came across a helpful ...

Determine time without discerning the timezone of the device

This script checks the current time against a specified deadline time. // Get current date/time var now = new Date(); // Set up deadline date/time var deadline = new Date(); //deadline is 1830hrs; deadline.setHours(18); deadline.setMinutes(30); // Chec ...

Updating is not happening with ng-repeat trackBy in the case of one-time binding

In an attempt to reduce the number of watchers in my AngularJS application, I am using both "track by" in ngRepeat and one-time bindings. For instance: Here is an example of my view: <div ng-repeat="item in items track by trackingId(item)"> {{ : ...

Creating a highly innovative server infrastructure tailored for Dojo applications with exceptional features

Recently, I have been using web2py for my projects and have found it incredibly useful in creating RESTful web applications. However, I am now looking to expand my skills in JavaScript by developing a more modern and dynamic client-side application that re ...

I am looking to adjust/modulate my x-axis labels in c3 js

(I'm specifically using c3.js, but I also added d3.js tags) I have been working on creating a graph that displays data for each month based on user clicks. However, I am facing an issue where the x-axis labels show 0-X instead of 1-X. For instance, ...

Having trouble with rendering prop values in Vue.js?

Struggling to develop an ajax form component in vuejs within a Laravel 5.3 application. It seems I am facing some challenges with displaying the form fields. Can someone lend me a hand? Here is the front end code for rendering the form: <ajax-form ...

Performing a GET call within the configuration settings of AngularJS

I found a similar question here but unfortunately it didn't solve my issue. I am currently using Angular-UI's Google Maps API, which requires configuration as follows: .config(function(uiGmapGoogleMapApiProvider) { uiGmapGoogleMapApiProvider ...

Having trouble with CSS text-align right not functioning properly? Here's how you can fix it

<div style="text-align: left;width: 21cm;"> <h4 style="text-align: center;font-weight: bold;font-size: 20px;margin: 0">Tax Invoice(Center) <span style="text-align: right;"> For Customer(Right)</span></h4> </div> I n ...

Having trouble getting npm install to add any libraries? Wondering how to fix this issue?

Currently, I am using Node version 14.15.5, npm version 6.14.11, and working on VS Code. I attempted to install two packages with the following commands: npm install express npm install lodash Unfortunately, neither installation was successful. This ...

Automatically updating the results section while executing SQL queries in PHP

Here is a JavaScript/Ajax code snippet: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready (function () { var updater = se ...

Handling multiple patch requests using React and Redux when onBlur event occurs

Currently, I am using Redux-form for editing guest information. Whenever a field is left, the content of that field gets patched to the guest through a simple patch request and the store is updated accordingly. However, an issue arises when I use Google fo ...

Add a Bootstrap Popover to the Amazon website

Currently, I am developing a straightforward chrome extension and my aim is to incorporate a basic bootstrap popover on a product page from amazon.com using my content.js script. To achieve this, I have implemented the following code within content.js for ...

Can you choose an option from a dropdown menu without relying on jQuery?

Can a dropdown list item be selected using AngularJS instead of jQuery? I have successfully accomplished this using jQuery, but I am curious if it can be done with AngularJS. This is how I achieved it using jQuery: var dropdownlist = $("select").data("k ...

A guide on updating the value from post.map back to the initial state in a React JS application

How can I change the value from the post.map into the value in the initial state for a homework problem? When I try to edit the form, it doesn't change because I am using the value from post.map. If I use the value in the initial state, the form is em ...

Preventing Excessive Event Triggering in a PhoneGap - Jquerymobile Application

I've created an application using a combination of Phonegap and JqueryMobile. So far, this app has accumulated approximately 15,000 downloads across iOS, Android, and iPhone platforms. Surprisingly, the code remains consistent across all versions. P ...