Compare commits
No commits in common. "b4afb10ef26e696ced13c13363f4c907a6bb3cb3" and "ea7d183da6215bcde0affe8c75572d10d2dff23c" have entirely different histories.
b4afb10ef2
...
ea7d183da6
|
@ -462,78 +462,14 @@ What can/cannot be serialized?
|
|||
|
||||
## 6.9 Object Methods
|
||||
|
||||
All JS objects (excpet explicitly created w/o prototype) inherits properties from `Object.prototype`. Hence they inherited some **primarily methods**, which are universally available.
|
||||
* `hasOwnProperty()`, `propertyIsEnumerable()`
|
||||
* `Object.create()`, `Object.keys()`
|
||||
* `toString`
|
||||
* ...
|
||||
|
||||
### 6.9.1 The toString() Method
|
||||
|
||||
* `toString()` takes no arg, and returns a string that represents the value of obj.
|
||||
* Default `toString()` is not useful (e.g. shown below), each class define their own `toString()`
|
||||
|
||||
```js
|
||||
let s = { x: 1, y: 1 }.toString(); // s evaluate to "[object Object]", without show real information
|
||||
```
|
||||
|
||||
* We can redefine `toString()` method:
|
||||
|
||||
```js
|
||||
let point = {
|
||||
x: 1,
|
||||
y: 2,
|
||||
toString: function() { return `(${this.x}, ${this.y})`; }
|
||||
};
|
||||
|
||||
String(point) // => "(1, 2)": toString() is used for string conversions
|
||||
```
|
||||
|
||||
### 6.9.2 The toLocaleString() Method
|
||||
|
||||
`toLocaleString()`:
|
||||
* All objs have this method
|
||||
* Purpose of this method: return a localized (vs. internationalization) string representation of the obj.
|
||||
* default `toLocaleString()` defined by Object call `toString()` directly
|
||||
* Date/Number class defin customized versions of `toLocaleString()` to format date, currency, time, etc.
|
||||
* User can create their own method:
|
||||
|
||||
```js
|
||||
let point = {
|
||||
x: 1000,
|
||||
y: 2000,
|
||||
toString: function() { return `(${this.x}, ${this.y})`; },
|
||||
toLocaleString: function() {
|
||||
return `(${this.x.toLocaleString()}, ${this.y.toLocaleString()})`;
|
||||
}
|
||||
};
|
||||
point.toString() // => "(1000, 2000)"
|
||||
point.toLocaleString() // => "(1,000, 2,000)": note thousands separators
|
||||
```
|
||||
|
||||
### 6.9.3 The valueOf() Method
|
||||
|
||||
`valueOf()`:
|
||||
* is called when JS needs to convert an obj to some non-string primitive type (e.g. number)
|
||||
* Many built-in class has its own `valueOf()`
|
||||
* Date class define its `valueOf()` to convert dates to number, so can perform comparison
|
||||
|
||||
### 6.9.4 The toJSON() Method
|
||||
|
||||
`toJSON()` method:
|
||||
* didn't get defined in `Object.prototype`;
|
||||
* `JSON.stringify()` looks for & invoke it on any object that will be serialized
|
||||
|
||||
```js
|
||||
let point = {
|
||||
x: 1,
|
||||
y: 2,
|
||||
toString: function() { return `(${this.x}, ${this.y})`; },
|
||||
toJSON: function() { return this.toString(); }
|
||||
};
|
||||
JSON.stringify([point]) // => '["(1, 2)"]'
|
||||
```
|
||||
|
||||
## 6.10 Extended Object Literal Syntax
|
||||
|
||||
### 6.10.1 Shorthand Properties
|
||||
|
|
|
@ -1,58 +0,0 @@
|
|||
# Chapter 8. Functions
|
||||
|
||||
**Function** = a block of JS code that's defined once but may be invoked multiple times.
|
||||
* *argument*: values provided by during invocation as function's parameters.
|
||||
* *return value*: computed by function using argument values
|
||||
* *invocation context*: value of `this` keyword for each invocation
|
||||
|
||||
Utilization of functions:
|
||||
* **method of object**: if function is assigned to a property of an obj.
|
||||
* When a function is invoked on an object (e.g. `obj.method()`), this object become invocation context (aka `this`) for the function
|
||||
* **Constructor**: function designed to initialize a new obj.
|
||||
|
||||
Function as **first class citizen** (object):
|
||||
* In JS, functions are obj. Hence, can be manipulated by programs
|
||||
* We can set properties on functions and invoke methods on them (i.e. pass function as parameter to method)
|
||||
|
||||
JS function definitions can be nested within other functions
|
||||
|
||||
## 8.1 Defining Functions
|
||||
|
||||
### 8.1.1 Function Declarations
|
||||
|
||||
**Function declaration** = `function` keyword + Identifier as function name + `(param1, param2, ...)` + `{JS statements as function body}`
|
||||
* `return` causes function to stop executing and return computed value to caller
|
||||
* if body has no `return`, value of function is `undefined`
|
||||
|
||||
```js
|
||||
// Print the name and value of each property of o. Return undefined.
|
||||
function printprops(o) {
|
||||
for(let p in o) {
|
||||
console.log(`${p}: ${o[p]}\n`);
|
||||
}
|
||||
}
|
||||
|
||||
// Compute the distance between Cartesian points (x1,y1) and (x2,y2).
|
||||
function distance(x1, y1, x2, y2) {
|
||||
let dx = x2 - x1;
|
||||
let dy = y2 - y1;
|
||||
return Math.sqrt(dx*dx + dy*dy);
|
||||
}
|
||||
|
||||
// A recursive function (one that calls itself) that computes factorials
|
||||
// Recall that x! is the product of x and all positive integers less than it.
|
||||
function factorial(x) {
|
||||
if (x <= 1) return 1;
|
||||
return x * factorial(x-1);
|
||||
}
|
||||
```
|
||||
|
||||
* Name of function (in function declaration) becomes a variable, whose value is function itself.
|
||||
* function declaration statements are "hoisted" (level up) to top of enclosing block.
|
||||
* All function in a JS block will be defined before JS interpreter start execution
|
||||
|
||||
### 8.1.2 Function Expressions
|
||||
|
||||
Function Expression (FE 函数表达式) vs Function Declaration (FD 函数声明):
|
||||
* FE appear within context of a larger expression
|
||||
*
|
Loading…
Reference in New Issue