Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why i can write a object like "c = { get a(){} }"?

Tags:

javascript

When I wrote

var c = { get a(){} };

The result is the c object has a "get a" and "set a" property.

Why?

Tested in Chrome and Firefox.

like image 513
Henry Avatar asked Nov 02 '22 14:11

Henry


1 Answers

Sometimes it is desirable to allow access to a property that returns a dynamically computed value, or you may want reflect the status of an internal variable without requiring the use of explicit method calls. In JavaScript, this can be accomplished with the use of a getter. It is not possible to simultaneously have a getter bound to a property and have that property actually hold a value, although it is possible to use a getter and a setter in conjunction to create a type of pseudo-property.

— https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/get

like image 131
Quentin Avatar answered Nov 15 '22 06:11

Quentin