JavaScript returning 'undefined' when calling prototype member

javascript function return undefined
javascript function return value to variable
javascript return html
javascript return nothing
javascript functions list
javascript function return multiple values
javascript return function object
javascript function return html string

So I'm new to OOP in JavaScript and having trouble understanding why I'm getting 'undefined' in the following code, please help:

function Vehicle(energyType, energy) {
	this.energyType = energyType;
	this.energy = energy;
}

function Car(energyType, energy) {
	Vehicle.call(energyType, energy);

	this.doors = 4;
}

Car.prototype = Object.create(Vehicle.prototype);

Vehicle.prototype.run = function() {
	console.log(`This vehicle is running on ${this.energyType}.`);
}

const c = new Car('gas', 80);

c.run();

When you .call, the first argument should be the this you want the called function to refer to. So,

Vehicle.call(energyType, energy);

results in Vehicle being called with one parameter, with the this value being what was initially the energyType variable (coerced into an object, because this has to be an object, not a primitive). You can see this if you console.log inside Vehicle:

function Vehicle(energyType, energy) {
  console.log('this', this);
  console.log('energyType', energyType);
  console.log('energy', energy);
	this.energyType = energyType;
	this.energy = energy;
}

function Car(energyType, energy) {
	Vehicle.call(energyType, energy);

	this.doors = 4;
}

Car.prototype = Object.create(Vehicle.prototype);

Vehicle.prototype.run = function() {
	console.log(`This vehicle is running on ${this.energyType}.`);
}

const c = new Car('gas', 80);

c.run();

JavaScript return Statement, A JavaScript function is a block of code designed to perform a particular task. A JavaScript return p1 * p2; // The function returns the product of p1 and p2 Definition and Usage. The return statement stops the execution of a function and returns a value from that function. Read our JavaScript Tutorial to learn all you need to know about functions. Start with the introduction chapter about JavaScript Functions and JavaScript Scope.

It's missing a this.

Vehicle.call(this, energyType, energy);

JavaScript Functions, A return statement determines the value the function returns. When control comes across such a statement, it immediately jumps out of the current function and  Function Return. When JavaScript reaches a return statement, the function will stop executing. If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement. Functions often compute a return value. The return value is "returned" back to the "caller":

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call

function.call(thisArg, arg1, arg2, ...)

Pass this in Vehicle.call(this,energyType, energy);

function Vehicle(energyType, energy) {
	this.energyType = energyType;
	this.energy = energy;
}

function Car(energyType, energy) {
	Vehicle.call(this,energyType, energy);

	this.doors = 4;
}

Car.prototype = Object.create(Vehicle.prototype);

Vehicle.prototype.run = function() {
	console.log(`This vehicle is running on ${this.energyType}.`);
}

const c = new Car('gas', 80);

c.run();

Functions :: Eloquent JavaScript, Rule #1. Every function in JavaScript returns undefined unless otherwise specified. Rule #3. The return statement returns a value to the function caller. Ending a Function. Since return stops a function's execution immediately, it can also be used to interrupt or end a function. Interrupting a Loop/Function. Returning b is returning a function object. In Javascript, functions are just objects, like any other object. If you find that not helpful, just replace the word "object" with "thing". You can return any object from a function. You can return a true/false value.

JavaScript: What is the return statement?, Javascript returning a function. 0. rainmaker April 5, 2018, 12:41am #1. Hello,. I am learning Javascript. I have completed basic understanding of javascript with  The || operator is a switch - it returns the first operand if it's not falsy or the 2nd operand if it is. – Jamie Treworgy Aug 27 '12 at 21:55 add a comment |

Javascript returning a function - JavaScript, Something that often trips up my students who are just learning is JavaScript is the return operator. Today, I want to give you a quick overview of  When you return something from a .then() handler, you can return either a value (which becomes the resolved value of the parent promise) or you can return another promise (which chains onto the previous promise) or you can throw which works like returning a rejected promise (the promise chain becomes rejected).

WTF is return in JavaScript, When to use return with JavaScript. One of the discussions that came up in my private Vanilla JS Slack channel (available to people who  parseFloat and parseInt can return NaN if the first character of the string cannot be converted to a number. So, I would safeguard against it like this (NaN is a falsy value): price = parseFloat(price) || 0; qty = parseInt(qty, 10) || 0; Arithmetic operations on numbers with the value NaN almost always result in NaN (NaN + 5 will result in NaN.)

Comments
  • lol.. funny how a simply typo can take you 30 minutes to figure out sometimes xD. THANK YOU