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();
Related
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)
This question already has answers here:
Why can I access TypeScript private members when I shouldn't be able to?
(10 answers)
Closed 2 years ago.
class Animal {
privateAttribute
setPrivateAttribute(value) {
this.privateAttribute = value
}
}
new Animal().setPrivateAttribute('good way') //ok
new Animal().privateAttribute = 'not allowed' // no
I want to prevent update privateAttribute directly, the only way to set privateAttribute is call setPrivateAttribute function. What shold I do?
Please put all the private variable inside your constructor.
class Animal {
constructor() {
let privateAttribute = 'default';
this.setPrivateAttribute = newValue => {
privateAttribute = newValue
}
this.getPrivateAttribute = () => privateAttribute;
}
}
let newAnimal = new Animal()
// get variable value
newAnimal.getPrivateAttribute()
// Set new Value
newAnimal.setPrivateAttribute('New Value')
This question already has answers here:
Use of 'prototype' vs. 'this' in JavaScript?
(15 answers)
Should I use prototype or not?
(3 answers)
Closed 4 years ago.
Lets say I have a class:
function Dog(name) {
this.name = name;
}
Dog.prototype = {
constructor: Dog
}
I would like to add some methods to that class, but I'm confused where I should add them, to the constructor itslef, or to the prototype of that class.
for example I can do:
function Dog(name) {
this.name = name;
this.bark = function() {
console.log(this.name + ' barked!');
}
}
Or I can alternatively do this:
function Dog(name) {
this.name = name;
}
Dog.prototype = {
constructor: Dog,
bark: function() {
console.log(this.name + ' barked');
}
}
What are the differences between the 2 options? which one is better? and which situations should I use each one?
Sidenote: I am aware this question is basic enough to likely have duplicates, but I cannot find any simillar posts, maybe I am constructing the question wrong.
This question already has answers here:
JSON stringify ES6 class property with getter/setter
(7 answers)
Closed 4 years ago.
According to Martin, this syntax can be used to define a class in nodeJS:
class Person {
constructor() {
this.id = 'id_1';
}
set name(name) {
this._name = name;
}
get name() {
return this._name;
}
usage:
myPerson.name = "Saul"; // save the value in `_name` variable.
Slight problem with this convention is that calling JSON.stringify(myPerson) will print the following:
{
"_name": "Saul"
}
But if I remove the underline from the setter function, and write it as the following:
set name(name) {
this.name = name;
}
the setter function will recursively call itself forever. So is there no way of using this nice getter-setter syntax, and still use the name property? I'm running nodejs.
I dont see a problem at all here. If you deserialize it, it works as expected:
const saul = Object.assign(new Person, JSON.parse('{"_name": "Saul"}'));
console.log(saul.name);
This question already has answers here:
Get function name in JavaScript
(7 answers)
Closed 4 years ago.
I have the following class
class MyClass {
constructor() {
}
doIt() {
var i = 10;
}
}
How, can I get the name of the class (string "MyClass") without creating instance of this class? I tried:
console.log(MyClass);
console.log(MyClass.constructor);
console.log(MyClass.constructor.name);
But can't get what I need.
Use the class property
class MyClass {
constructor() {
}
doIt() {
var i = 10;
}
}
console.log(MyClass.name)
Working fiddle: https://jsfiddle.net/ghdb6ebc/1/