mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-25 11:12:21 +00:00
Fix coding style in docs/class.md
This commit is contained in:
parent
307932e155
commit
96b4a4a33d
@ -14,7 +14,7 @@ function Point(x,y){
|
|||||||
|
|
||||||
Point.prototype.toString = function () {
|
Point.prototype.toString = function () {
|
||||||
return '(' + this.x + ', ' + this.y + ')';
|
return '(' + this.x + ', ' + this.y + ')';
|
||||||
}
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
上面这种写法跟传统的面向对象语言(比如C++和Java)差异很大,很容易让新学习这门语言的程序员感到困惑。
|
上面这种写法跟传统的面向对象语言(比如C++和Java)差异很大,很容易让新学习这门语言的程序员感到困惑。
|
||||||
@ -74,7 +74,7 @@ class Point {
|
|||||||
Point.prototype = {
|
Point.prototype = {
|
||||||
toString(){},
|
toString(){},
|
||||||
toValue(){}
|
toValue(){}
|
||||||
}
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
在类的实例上面调用方法,其实就是调用原型上的方法。
|
在类的实例上面调用方法,其实就是调用原型上的方法。
|
||||||
@ -100,7 +100,7 @@ class Point {
|
|||||||
Object.assign(Point.prototype, {
|
Object.assign(Point.prototype, {
|
||||||
toString(){},
|
toString(){},
|
||||||
toValue(){}
|
toValue(){}
|
||||||
})
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
`prototype`对象的`constructor`属性,直接指向“类”的本身,这与ES5的行为是一致的。
|
`prototype`对象的`constructor`属性,直接指向“类”的本身,这与ES5的行为是一致的。
|
||||||
@ -133,11 +133,11 @@ Object.getOwnPropertyNames(Point.prototype)
|
|||||||
```javascript
|
```javascript
|
||||||
var Point = function (x, y){
|
var Point = function (x, y){
|
||||||
// ...
|
// ...
|
||||||
}
|
};
|
||||||
|
|
||||||
Point.prototype.toString = function() {
|
Point.prototype.toString = function() {
|
||||||
// ...
|
// ...
|
||||||
}
|
};
|
||||||
|
|
||||||
Object.keys(Point.prototype)
|
Object.keys(Point.prototype)
|
||||||
// ["toString"]
|
// ["toString"]
|
||||||
@ -585,7 +585,7 @@ var obj = {
|
|||||||
toString() {
|
toString() {
|
||||||
return "MyObject: " + super.toString();
|
return "MyObject: " + super.toString();
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
obj.toString(); // MyObject: [object Object]
|
obj.toString(); // MyObject: [object Object]
|
||||||
```
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user