Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   get/set в Javascript (https://javascript.ru/forum/misc/81921-get-set-v-javascript.html)

denis_alekss 14.02.2021 17:30

get/set в Javascript
 
Решил понять смысл get/set, написал небольшой код, правильно ли в нем используется метод get/set или нужно по-другому, если по-другому, напишите пож. как лучше в моем коде.

<script>
class Beverage{
	constructor(options){
		this.name = options.name;
	}
	returnName() {console.log(this.name)}
}

class Price extends Beverage
{
	constructor(options){
		super(options)
		this.price = options.price;
		this.bos = options.bos;
	}

	result() {console.log("Напиток " + this.name + " Цена" + this.price + "Директор - " + this.bos)}
	
	get Bos(){
	{return result()}
    }	
	
    set Bos(newBos){
	const newbos = newBos.split(' ');
	this.bos = newbos[0];
	this.bos = newbos[1]
   }	
}

const bev = new Beverage({name: 'Coca-cola'})
bev.returnName()

const price = new Price({name: 'Fanta',price: " - 200 евро ",bos: "Стивен"})
price.result()

price.bos = "Майкл Джексон"
price.result()
</script>


Часовой пояс GMT +3, время: 17:06.