Can You help with this thing that I need to figure out. I started learning Js with OOP but I am kind of stuck with this, where am I making a mistake. This is the assignment I have to figure out
Create a class Car with a property that holds a number of doors and one method that prints the number of doors to the console.
class Car {
constructor(doors){
this.doors=doors
console.log(doors)
}
}
you need to create a method in the Car class to print the number of doors and then you need to instantiate the class with a given number of door & then call that method on it.
class Car {
constructor(doors){
this.doors = doors;
}
print(){
console.log(this.doors);
}
}
const bmw = new Car(4);
bmw.print()
Hey 👋
Yeah no problem :)
class Car {
constructor(doors) {
this.doors = doors;
}
printDoors() {
console.log(this.doors);
}
}
In JS OOP you have to define your member variables within the constructor by using the this keyword.
To access your variables somewhere else in the class you also have to use `this.
The printDoor() function has to be defined at its own to call it later on like this:
const numberDoors = 4;
const myCar = new Car(numberDoors);
myCar.printDoors();
// expected output: 4
Related
I'm trying to build a complex class where I want to group properties, making the instantiated object have multiple layers, instead of every property being at root level.
So far, the only way I've found to do this is by making a class with the properties to group, and then in a "parent" class add a property of the class I built.
The problem here though is that two properties not sharing the same class can't communicate with each other.
There are ways around this, but I find them all very hacky and looking bad. One would be to create a hidden element, and store data in there that a property from another class can read.
Another would be to create static properties, but then, unless you do some major work with that property, you can only have one object created from the parent class, as it'll be the same no matter the instantiation of the class.
Very basic example:
class A {
constructor(prop1){
this.property = prop1;
}
}
class B {
constructor(prop2){
this.property = prop2;
}
}
class C {
constructor(prop1, prop2){
this.PropertyA = new A(prop1);
this.PropertyB = new B(prop2);
}
}
let obj = new C(1, 1);
console.log(obj.PropertyA.property);
In this example, the property from class A can't get a value from property in class B.
So, my question is, is there another way of building the class C to keep the levels of hierarchy in the object?
I use the class structure because I like how it looks. It looks far more readable to me than the prototype structure, and I'm not building an object directly, as I would like to instantiate more of them.
It feels like I have forgotten things I've looked at to try to do this, but I'm sure it'll come to me soon enough after I post this.
Sooo...
I worked a bit on a static-solution, and basically made a private static property to hold a unique id per instantiated object, with the key-value pairs I want to be able to share between the different classes. This should only expose the methods to either set or get those values. The only requirement is that all the classes needs to be constructed with the object ID, so they can get the right value.
I understand that people will roll their eyes at my infantile tries to break the actual points of classes and such, but it works for me anyway in this specific circumstance anyway.
I'm sure there a multitude of ways to update it to ensure it runs more smoothly, but I think it works for most cases at the moment.
The code made in example code:
"use strict";
class A {
#id
#testProp
constructor(id){
this.#id = id;
this.#testProp = 10;
}
get TestProp(){ return this.#testProp + C.getSharedProp(this.#id, "BValue")};
set TestProp(newValue) { this.#testProp = newValue; C.setSharedProp(this.#id, "AValue", this.#testProp) };
}
class B {
#id
#testProp
constructor(id){
this.#id = id;
this.#testProp = 10;
}
get TestProp(){ return this.#testProp + C.getSharedProp(this.#id, "AValue")};
set TestProp(newValue) { this.#testProp = newValue; C.setSharedProp(this.#id, "BValue", this.#testProp) };
}
class C {
#id
constructor(){
this.#id = Math.random().toString(36).substr(2, 9);
this.PropertyA = new A(this.#id);
this.PropertyB = new B(this.#id);
}
static #sharedProps = {};
static getSharedProp(charId, valueName) {
if(!charId){
throw "Must supply character ID";
}
if(!valueName){
throw "Must supply name of value to return";
}
if(!(charId in this.#sharedProps)){
throw "Character ID not found";
}
if(!(valueName in this.#sharedProps[charId])){
throw valueName + "-element not found";
}
return this.#sharedProps[charId][valueName];
}
static setSharedProp(charId, valueName, value) {
if(!charId){
throw "Must supply character ID";
}
if(!valueName){
throw "Must supply name of value";
}
if(!(charId in this.#sharedProps)){
this.#sharedProps[charId] = [];
}
if(!(valueName in this.#sharedProps[charId])){
this.#sharedProps[charId][valueName] = -1;
}
if(!value){
console.warn("Value not supplied of " + valueName + ". Not updating extant value");
}else{
this.#sharedProps[charId][valueName] = value;
}
}
}
let obj = new C();
obj.PropertyA.TestProp = 20;
obj.PropertyB.TestProp = 5;
console.log(obj.PropertyA.TestProp); //should be 25; 20 from its own class and 5 from foreign class-object
console.log(obj.PropertyB.TestProp); //should be 25; 5 from its own class and 20 from foreign class-object
I have Student class. The student has a grade.
Teacher class can change the student's grade.
Other classes (i.e., parent class) cannot do that. I think I get the part about how to change another class's property (please correct me if I'm wrong), but how to make sure that only the Teacher class can change the grade?
class Student {
grade = 'A';
changeGrade(grade) {
this.grade = grade
return `the new grade is ${this.grade}`
}
}
class Teacher {
changeStudentGrade(student, grade) {
return student.changeGrade(grade)
}
}
JavaScript does not support this functionality. However, you can still add suitable logic to facilitate it by checking the class that is trying to changeGrade within the method as follows:
changeGrade(grade, classChangingGrade) {
if (classChangingGrade instanceof Teacher) {
this.grade = grade
return `the new grade is ${this.grade}`
}
}
Then this method should be invoked as below:
return student.changeGrade(grade, this);
Disclaimer
Other classes can get around this by creating a new Teacher instance and invoking changeGrade as follows:
student.changeGrade(grade, new Teacher());
I am pretty new to programming so I am not sure what this is called but in Javascript, for example
arr[0].obj[0].getSomething();
can be shorten to
var o = arr[0].obj[0];
o.getSomething();
so that we do not have to repeat
arr[0].obj[0]
what is the equivalent of this in Java? I can't seem to find it.
Also tell me what should the title be, I am not sure whether my title is appropriate.
You do it just the same way:
If you have
House[] houses; // An array of houses
// initialize and fill the array
... and inside the House class you have a field doors:
public class House {
Door[] doors;
// Initialize the array in a constructor, add getter and setter methods
}
Then you can do either
Color doorColor = houses[0].doors[0].getColor();
or you store the door you want in a variable and then ask for its color:
Door door = houses[0].doors[0];
Color doorColor = door.getColor();
With the given snipped I can understand your java code would look like as below.
class A{
B obj[];
}
class B {
public void doSomething(){}
}
lets say you have array of A class object as as your code snippet says
arr[0].obj[0].getSomething();
in java it would be of class A like below
A arr[]
so the code would be
arr[0].obj[0].doSomething();
we can write it as
A firstA = arr[0];
B firstB = firstA.obj[0];
firstB.doSomething();
or
B firstB = arr[0].obj[0];
firstB.doSomething();
This should be straight-forward - creating a simple class within ECMA
class Sandwich
{
constructor(filling)
{
this.sandwichname = filling;
}
}
mysandwich = new Sandwich("Peanut Butter");
Photoshop says Error 9: Illegal used of reserved word 'class' line 1.
Only I'm sure there is a way to create class - just not with this type of construction, if you pardon the pun.
You can't use this version of javascript. However, it is possible to emulate the functionality like so:
function Sandwich(filling) {
this.filling = filling;
}
Sandwich.prototype.someMethod = function() {
console.log(this.filling);
};
var jelly = new Sandwich("jelly");
jelly.someMethod();
I'm having a difficulty to understand
how the class feature works out.
Can you super simplify it to me the following snippet
with simple everyday words?
I really find it hard to grasp the whole thing.
That super function looks very weird, also.
Thank you in advance.
class Dessert {
constructor(calories = 250) {
this.calories = calories;
}
}
class IceCream extends Dessert {
constructor(flavor, calories, toppings = []) {
super(calories);
this.flavor = flavor;
this.toppings = toppings;
}
addTopping(topping) {
this.toppings.push(topping);
}
}
I think easiest to understand is just to create a new instance of IceCream:
var iceCream = new IceCream('vanilla');
console.log(iceCream) // IceCream {calories: 250, flavor: "vanilla", toppings: Array(0)}
As you see, you wasn't needed to pass calories value - super calls parent class and takes that value.
When you have a constructor function, and instantiate new instance constructor method being fired first, to collect properties as you describe in it.
Comment, please, if something is unclear. I would love to provide more info.