class
This commit is contained in:
parent
e7d6d4a8db
commit
dd106baf4c
1 changed files with 2 additions and 20 deletions
|
@ -232,7 +232,7 @@ new User().sayHi(); // Hello
|
|||
|
||||
## Getters/setters, other shorthands
|
||||
|
||||
Classes also include getters/setters, generators, computed properties etc.
|
||||
Just like literal objects, classes may include getters/setters, generators, computed properties etc.
|
||||
|
||||
Here's an example for `user.name` implemented using `get/set`:
|
||||
|
||||
|
@ -325,24 +325,6 @@ The property is not placed into `User.prototype`. Instead, it is created by `new
|
|||
|
||||
## Summary
|
||||
|
||||
JavaScript provides many ways to create a class.
|
||||
|
||||
First, as per the general object-oriented terminology, a class is something that provides "object templates", allows to create same-structured objects.
|
||||
|
||||
When we say "a class", that doesn't necessary means the `class` keyword.
|
||||
|
||||
This is a class:
|
||||
|
||||
```js
|
||||
function User(name) {
|
||||
this.sayHi = function() {
|
||||
alert(name);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
...But in most cases `class` keyword is used, as it provides great syntax and many additional features.
|
||||
|
||||
The basic class syntax looks like this:
|
||||
|
||||
```js
|
||||
|
@ -363,6 +345,6 @@ class MyClass {
|
|||
}
|
||||
```
|
||||
|
||||
`MyClass` is technically a function, while methods are written to `MyClass.prototype`.
|
||||
`MyClass` is technically a function (the one that we provide as `constructor`), while methods, getters and settors are written to `MyClass.prototype`.
|
||||
|
||||
In the next chapters we'll learn more about classes, including inheritance and other features.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue