This question already has an answer here:
Javascript call Parent constructor in the Child (prototypical inheritance) - How it works?
(1 answer)
Closed 9 months ago.
I'm trying to use prototypal inheritance and its not working, everytime i try i keep getting undefined:
function HospitalEmployee(name) {
this._name = name;
this._remaningVacationDays = 20;
}
function Nurse() {}
Nurse.prototype = Object.create(HospitalEmployee.prototype)
const nurseOlynyk = new Nurse("Olynyk", ["Trauma", "Genetics"])
console.log(nurseOlynyk._remainingVacationDays) //Prints undefined
What am i doing wrong?
I need use Class and Extends for it same as :
class HospitalEmployee {
constructor(name){
this._name = name;
this._remaningVacationDays = 20;
}
}
class Nurse extends HospitalEmployee {
constructor(name, otherParams){
super(name);
this.otherParams = otherParams;
}
}
const nurseOlynyk = new Nurse("Olynyk", ["Trauma", "Genetics"])
console.log(nurseOlynyk)
Related
This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
How does the "this" keyword in Javascript act within an object literal? [duplicate]
(4 answers)
Closed 1 year ago.
when it comes to this in javascript classes it suppose to refer to the class instances but at
somechildmethod() method it don't it throws an error saying TypeError: Cannot read property 'name' of undefined and I know this error will go away if I use the arrow function but I want to know why please!
class parent {
constructor(name, score) {
this.name = name;
this.score = score;
}
someparentmethod() {
console.log("Hi!");
}
}
class child extends parent {
constructor(name, score, thirdprop) {
super(name, score);
this.thirdprop = thirdprop;
}
somechildmethod() {
return function () {
console.log(this.name);
};
}
}
const child1 = new child("zahal", 1);
const child2 = child1.somechildmethod();
child2();
This question already has answers here:
Getters \ setters for dummies
(14 answers)
Closed 3 years ago.
I try to use setter method with my class Test but my log return "TypeError: testObject.id is not a function"
class test {
constructor() {
this._id = 0;
}
get id(){
return this._id;
}
set id(newId){
this._id = newId;
}
}
const testObject = new test();
console.log(testObject.id); // return 0
testObject.id(12); //return TypeError: myTest.id is not a function
console.log(testObject.id);
I expect the ouput will be 12 but i obtain TypeError.
To use a setter, you do assignment, you don't write a function call:
testObject.id = 12;
Live Example (I've also changed the name to Test; the overwhelming convention in JavaScript is to capitalize constructor functions):
class Test {
constructor() {
this._id = 0;
}
get id(){
return this._id;
}
set id(newId){
this._id = newId;
}
}
const testObject = new Test();
console.log(testObject.id); // 0
testObject.id = 12;
console.log(testObject.id); // 12
This question already has answers here:
Javascript ES6 class definition not accessible in window global
(2 answers)
Closed 3 years ago.
class Unit {
constructor(){
}
}
var str = "Unit";
var a = new window[str](); // error
var b = new window["Unit"](); // error
var u = new Unit(); // works
(u instanceof Unit) // true
I only recently made the switch to ES 6 syntax in regards to declaring classes. Im pretty sure that formerly i could instantiate a class like this, however ever since i have used "class" syntax, the instantiation of an object by windowclassName is not working anymore.
What exactly am i missing here ?
Variables declared with class behave similarly to those declared with const and let - they do not get implicitly assigned to the window object (which is arguably a good thing). If you wanted to put Unit on window, you would have to do so explicitly:
class Unit {
constructor(){
console.log('constructing');
}
}
window.Unit = Unit;
var str = "Unit";
var a = new window[str]();
You might consider using your own object of classes to avoid global pollution:
const myClasses = {
Unit: class Unit {
constructor(){
console.log('constructing');
}
}
};
var str = "Unit";
var a = new myClasses[str]();
Object values cannot reference each other while in the process of declaring an object literal - to put a subclass on myClasses that extends one of the existing classes, you'll have to do so outside of the myClasses declaration:
const myClasses = {
Unit: class Unit {
constructor(){
console.log('constructing');
}
}
};
myClasses.Child = class Child extends myClasses.Unit {
constructor() {
super();
console.log('Child running');
}
}
var str = "Child";
var a = new myClasses[str]();
This question already has answers here:
React.Component vs React.createClass
(5 answers)
Closed 4 years ago.
Im new to JS and react, and just curious about a few (probably simple) things are. Im trying to make reusable radio buttons using exports and imports in reactJS.
Im trying to understand the difference between these two things is
var RadioInput = React.createClass({
render: function() {
.....
};
});
and
export class RadioInput extends Component {
render(){
....
};
}
is.
Thanks! Sorry if this is a ridiculous question
Regarding javascript, a class is just a "special type of function", syntactic sugar for the prototype inheritance using constructors, so the following statements serve the same purpose:
// Constructor Function
function Dog() {
this.bark = "woof"
}
// Class Syntax
class Dog {
constructor(){
this.bark = "woof";
}
}
Hence, you can assign a class to a variable the same way you assign a function:
const Dog = function() {
this.bark = "woof";
}
const Dog = class {
constructor() {
this.bark = "woof";
}
}
This question already has answers here:
Private properties in JavaScript ES6 classes
(41 answers)
Closed 7 years ago.
Hi I have the following class definition:
'use strict'
class Human {
constructor(name, age) {
this._name = name || null;
this.age = age || 'no age defined';
this.rights = ['Human Rights'];
}
get this() {
return 'access denied';
}
set name(name) {
this._name = this._name ? this._name : name;
}
get name() {
return this._name;
}
}
let me = new Human();
console.log(me); // this should return a string "access denied"
I thought it might be possible to define a getter for the whole instance. But this is not, or?
Does anyone know anything about that? Or is there an alternative methods to create restricted Classes?
kind regards
martin
No, but what you could do is put some code in a module and only export part of it.
// human.js
class SecretHuman {
constructor(name) {
this.name = name;
}
}
let human = new SecretHuman('Bob');
export class Human {
secret() {
console.log(`Secret human is ${human.name}`);
}
}
// test.js
import {Human} from './human';
new Human().secret();