Constructor function fails to display accurate outcomes

Just beginning my coding journey here. Attempting to create a car object and its instances. Here's the code snippet:

  function Car(name, speed) {
        this.name = name;
        this.speed = speed;
        this.describe=describeCar;
    };


    function describeCar(){
        document.write("The Full name is " + this.name + " and the top speed is " + this.speed);
    };

    var mercedes = new Car("Mercedes benz", 233); // Instance of Object
    var bmw = new Car("British motor Works", 260); // Instance of Object
    mercedes.describeCar();

However, upon running the code, nothing appears in the browser. I'd really appreciate any guidance on what I might be missing or doing incorrectly. Thanks a bunch!.

Answer №1

You are linking the function describeCar to the describe property of the object Car, allowing you to use it in this way:

mercedes.describe();

Answer №2

function Automobile(brand, velocity) {
        this.brand = brand;
        this.velocity = velocity;
        this.info = describeAuto;
    };

    function describeAuto(){
        document.write("The brand is " + this.brand + " and the top speed is " + this.velocity);
    };

    var toyota = new Automobile("Toyota", 180); // Instance of Object
    var honda = new Automobile("Honda", 205); // Instance of Object
toyota.info();

honda.info();

Remember to call the class method name instead of the specified one.

Answer №3

When using the describeCar function, remember to include parentheses when calling it and use "this" as a reference if you intend to utilize it within the constructor:

function Car(name, speed) {
    this.name = name;
    this.speed = speed;
    this.describe=this.describeCar();
};

Car.prototype.describeCar = function(){
    return "The Full name is " + this.name + " and the top speed is " + this.speed;
};

var mercedes = new Car("Mercedes benz", 233); // Object Instance
var bmw = new Car("British motor Works", 260); // Object Instance
document.write(mercedes.describe);

http://jsfiddle.net/nzv7jjch/

If you only require the function output, simply call the function without using it in the constructor:

function Car(name, speed) {
    this.name = name;
    this.speed = speed;
};

Car.prototype.describeCar = function(){
    return "The Full name is " + this.name + " and the top speed is " + this.speed;
};

var mercedes = new Car("Mercedes benz", 233); // Object Instance
var bmw = new Car("British motor Works", 260); // Object Instance
document.write(mercedes.describeCar());

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

Encountering challenges with concealing a div element

As I'm setting up a table, I want to hide it immediately after creating it without affecting the DOM. Then, when the user selects from a dropdown menu, I show the table and everything works fine. However, the issue arises when I visit the page for the ...

Eliminating a sub-array within an array

This is the issue at hand: A function named filteredArray has been created. It takes two arguments - arr, a nested array, and elem. The purpose of this function is to return a new array by filtering out any nested arrays within arr that contain the specifi ...

Having trouble with the authorization aspect of Next Auth. The error message reads: "NextAuth.js does not support the action api with HTTP GET method

Recently, I've been encountering a puzzling error with my Next.js app authentication. It seems that I am unable to authenticate with the provided credentials. After reviewing the documentation, everything appears to be correct on my end. Additionall ...

The copying of array values is not supported by Chromium

While utilizing Webworker to process an array of pixels from Canvas and then assign it back, I encountered a strange behavior. Firefox works flawlessly in this scenario, however, Chromium seems to insert an empty pixel array into the Canvas. Upon further ...

What is the best way to retrieve data using an Ajax rest call from an API on my HTML webpage?

I am currently working on a Javascript application where I aim to gather football/soccer statistics from football-data.org and display specific information on my HTML Page. Despite my efforts to retrieve the data, I am facing challenges in making an AJAX ...

Discover the best way to reference a JavaScript variable within an HTML form textfield

I'm having trouble with a script that is supposed to display the selected value from a drop down list in a text field on an HTML form. When I select an option, the value is not appearing in the text field. Can someone please assist me with this issue? ...

Is there a method for configuring NextAuth to utilize the /src/app directory instead?

Is there a specific method to instruct NextAuth to redirect to this particular directory, but within the src/app directory? All of my routes are now located there due to the changes in Next.js 13. Below is an example of the code I am using: export const a ...

Sometimes, JQuery struggles to set input values accurately

Exploring the single page app sample provided here, I have encountered some anomalies when attempting to manipulate input controls with JQuery under certain conditions. Below is the consistent HTML structure followed by the JavaScript snippets in question. ...

Implement a customized toString method for components in ReactJS

Looking to modify the toString method of a class component in reactjs? Check out the code snippet below class C1 extends React.Component{ render(){ return ( <div> {C2.toString()} </div> ) } } class C2 extend ...

Direct your cursor to move across the sphere surface in THREE.JS

I'm working on a project to develop an interactive globe where users can drag points on the surface using their mouse cursor. Here is the code snippet I've been working on Currently, I've managed to set up the main sphere and a smaller red ...

Implementing a JavaScript file and ensuring W3C compliance

I recently purchased a template that included a javascript file in the main page with the following code: <script src="thefile.js?v=v1.9.6&sv=v0.0.1"></script> Upon inspection, I noticed there are two arguments at the end of the file ...

Survey Vue having trouble with indexing questions

I am in the process of developing a quiz/survey application using vue.js. As someone who is new to vue, I am still learning about its functionalities. The quiz/survey that I am creating will present different questions based on the user's responses to ...

What are the steps to changing the navbar's color when scrolling down?

I am having trouble changing the color of my navigation bar from transparent to black after the user scrolls down. Despite trying various methods and watching tutorials, I can't seem to get it to work. HTML <nav role='navigation' class= ...

Is it possible to refresh AdSense banner when the router changes?

Is there a way to reload the AdSense banner ads when the router changes? I've been encountering issues trying to re-add the script and HTML properly. Any guidance on how this should be done would be greatly appreciated... This is just a test for one ...

Designing a menu inspired by the layout of Google Plus

I am attempting to create a responsive menu that behaves similarly to the one on Google Plus. In this menu, main options are either added to or removed from the "more" dropdown as the window is resized. Here is the current appearance of my menu: Below is ...

Having difficulty grasping the concept of MVC within the context of my website's code

I'm struggling to grasp the concept of utilizing model-view-controller in the context of my registration system. As far as I understand it, the view loads, displaying the registration HTML form to the user. Once the user submits the form, a JavaScript ...

Issues with Minor Code Problems in Node.js - Need for Exported Functions

Following a Previous Update: We Are Invoking a Function in this Way var lastUsedRoute = appNavigator.getLastUsedRoute(); Where appNavigator is var appNavigator = require("app_utils/default/app-navigator"); and app-navigator includes an export like the fo ...

Troubleshooting 'Warning: Prop `id` did not match` in react-select

Having an issue with a web app built using ReactJs and NextJs. I implemented the react-select component in a functional component, but now I'm getting this warning in the console: Warning: Prop id did not match. Server: "react-select-7 ...

Turn off the Ripple effect on MUI Button and incorporate a personalized touch

I am trying to create a custom click effect for the MUI Button by removing the default ripple effect and implementing my own shadow effect. I have disabled the ripple using disableRipple, but I am facing issues in applying the shadow effect when the user c ...

Expanding Form Fields with jQuery: A Step-by-Step Guide

I am having trouble figuring out how to add and remove input fields dynamically on my webpage. I have three input fields with an "+Add More" button, but I can't quite grasp the logic for adding or removing these fields. Currently, I am using HTML and ...