Unable to append a property to a prototype in JavaScript

Experimenting with prototypes to enhance my understanding, I decided to test it in the console:

    function Dog(){}

    Dog.prototype.breed = breed;

    Dog.prototype.talk  = function(){

    console.log('I\'m a ' + this.breed);

    };


    dog1 = new Dog();


    dog1.breed = 'poodle';

    dog1.talk();

    //Here's the error message that came up...
    //ReferenceError: breed is not defined

Update: I replaced null and everything worked smoothly. Interestingly, using Dog.prototype.name = name; actually works!

    Dog.prototype.name = name;
    //Dog.prototype.breed = null;
    Dog.prototype.talk  = function(){

    console.log('my name is ' + this.name );

    };


    dog1 = new Dog();

    dog1.name = 'charly';
    //dog1.breed = 'poodle';

    dog1.talk();

Answer №1

Define the breed of Dog by setting the prototype property:
Dog.prototype.breed = breed;

To avoid any errors, make sure to set a default value for the breed property:

If the breed is not specified, default it to "mutt":
Dog.prototype.breed = "mutt";

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

Sending an ArrayBuffer from Angular to Electron results in the window crashing

In my Electron application, I have integrated an Angular application internally. I am trying to download a byte array (ArrayBuffer) via an API call and then passing this data to a method connected through electron.remote.require('./file-service') ...

Showing dummy data in demo mode with an AngularJS table

I stumbled upon this interesting jsfiddle http://jsfiddle.net/EnY74/20/ that seems to be using some demo data. If you check out this example https://jsfiddle.net/vsfsugkg/2/, you'll notice that the table originally has only one row, but I modified it ...

Changing the value of an element using JavaScript through Selenium WebDriver in .NET

I'm attempting to perform a popup page test using the chrome webdriver and selenium2 with .NET, but I am encountering some difficulties. After the window pops up, I need to modify the value of an element. Specifically, I need to change the default val ...

Unable to execute mongoose operation on database collection due to certain restrictions

Currently utilizing Mongoose for communication with MongoDB. Running into issues while attempting to execute certain operations. The database contains a collection named USERS, characterized by the following schema: {username: 'user1', id: 1, l ...

Issues with receiving incorrect orders and implementing switch case statements in JavaScript

Attempting to utilize a switch-case structure based on months. For example, if the user selects 1, it should display January. The months are stored in an array where arr[0] = January. However, when selecting 1 from the dropdown, it displays "january" inste ...

What steps are involved in creating a circular shape on canvas?

I am trying to create a circular shape in the canvas using the code below. The code involves moving an object with keyboard keys, and I need help making the canvas shape into a circle without affecting the functionality of the code. I attempted to modify t ...

Troubleshooting Issue with Jquery Ajax Success Event and Retrieving Response Message

I've encountered a problem while calling the jQuery Ajax function. It seems to be working fine, except for the fact that it's not receiving any response and appending it in. Upon submitting the form, the beforeSend event is triggered successfull ...

Displaying content generated by Html.Raw in MVC4 using JavaScript and AJAX is causing displacement of content within the <a> tags in the HTML

My project includes a feature where emails sent to a specific address are parsed, with attachments saved to the network and metadata stored in a database. If the serial number of a finished good matches the one included in the email, a note is generated an ...

Is it possible to create a Morris.js Bar Graph using PHP?

I've been trying to showcase my MySQL data using Morris.js graphs, but unfortunately, the graphs are not showing up at all. I carefully followed a basic tutorial, yet nothing seems to be working. Can someone please point out what may be going wrong wi ...

Creating a custom HTML5 canvas with images from a JSON array

I am currently working on a project to develop a unique slideshow feature by hosting images in an external JSON array and then loading them onto an HTML page. The next step is to insert these images onto an HTML5 canvas. While I have successfully loaded a ...

What is the process for displaying all indexes of a Mongo Collection using MongoDB Node native?

I am curious about how to retrieve all indexes from a specific collection in mongodb. I've tried using listIndexes, indexes, and indexInformation, but these methods only return empty values (arrays and objects). However, when I run db.getCollection(&a ...

The functionality of $watch is not functioning correctly within the Modal, despite implementing it with $scope.user={pwd1:''}

For the Plunker link referenced, please click here: http://plnkr.co/edit/0vOjw0?p=preview index.html <!DOCTYPE html> <html ng-app="app"> <head> <link rel="stylesheet" href="style.css"> ...

Bootstrap form validation solution

Utilizing bootstrap validation to validate a jsp page. The folder structure is as follows: WebContent ├── bootstrap-form-validation ├── js └── pages All three folders are under the web content. If I create another folder called teacher ...

Looking to create a calendar feature that includes time slot options for every date, which will correspond with data pulled from a JSON response

I am in the process of creating a calendar component for a React/Redux project. My goal is to retrieve a list of dates with specific times from an API. The available time slots are limited to AM, PM, ANY, each with a unique id associated with them. I am ...

Could not add metric widgets in Ambari

Having trouble adding metrics widgets for a component I created in Ambari. Any help would be appreciated... Running Ambari version 2.5.2 and encountered the following errors: On opening the component page: app.js:66933 Uncaught TypeError: Cannot read p ...

Node.js server continues running after attempting to stop with ctrl + C following starting the server using the command "npm start"

Whenever I initiate my server by typing node app.js in the command line on Git Bash, I can stop it simply by using ctrl + C. In my package.json file, I have configured a start script that allows me to use the command npm start to kickstart the server: "s ...

How can a new <li> element be created without being influenced by certain CSS styles?

I am attempting to enhance my menubar by adding a signup/login item aligned on the right without affecting the other center-aligned items. Essentially, I have an entry named "Login/Sign Up" that should maintain its own unique style while seamlessly blendin ...

Having trouble incorporating a react component I discovered into my application. Encountering an error message stating "Attempted import error."

My aim is to integrate an existing react component (shown below) that I came across on the internet into my application for use, specifically an animated navigation bar. Upon attempting to compile the code snippets below (snippets 2 and 3), I encountered ...

When a useContext is wrapped in a hook and bundled to commonJS, it results in an undefined value

Issue: Upon bundling, the code ceases to function properly. I have a hook that consumes a context and a component that relies on this hook: const useAContext = () => { return useContext(AContext) } const SomeComponent = () => { const aContex ...

What is the process for including a callback in an angular resource?

In my current setup, I have a view that displays a list of items: <li ng-repeat="item in items">{{item.name}}</li> The associated service code is as follows: angular.module('itemService', ['ngResource']). factory(&apo ...