文章目录
  1. 1. Arrow Functions

Do Not Use ==

1
2
null === undefined //false
null == undefined //true

Javascript’s Objects are dictionaries that map property names to values.You can create an object by calling a constructor.

1
2
p = new Object()
p.x //undefined

Property name are always strings.

1
2
3
4
5
6
7
8
9
10
var daddysShirt = {
type: 'polo',
color: 'white',
numButtons: 3
}
console.log(daddysShirt.color);
delete daddysShirt.color
console.log(daddysShirt.color);
//white
//undefined

Null is not an object

Delegation

1
2
3
4
5
6
7
var myShirt = Object.create(daddysShirt)
myShirt.color //white
myShirt.color = "green" //green
delete myShirt.color
myShirt.color //"white"
delete myShirt.numButtons //nothing happen
myShirt.numButtons //3
1
2
3
4
5
6
function add(){}
console.log(add.name)
var plus = add
console.log(plus.name)
//add
//add

Arrow Functions

An arrow function expression (also known as fat arrow function) has a shorter syntax compared to function expressions and lexically binds the this value (does not bind its own this, arguments, super, or new.target). Arrow functions are always anonymous

In some functional patterns, shorter functions are welcome.

1
2
3
4
5
6
7
8
9
10
11
12
var a = [
"Hydrogen",
"Helium",
"Lithium",
"Beryllium"
]
var a2 = a.map(function(s){ return s.length});
var a3 = a.map(s => s.length)
console.log(a2);
console.log(a3);
//[ 8, 6, 7, 9 ]
//[ 8, 6, 7, 9 ]

Until arrow functions, every new function defined its own this value (a new object in case of a constructor, undefined in strict mode function calls, the context object if the function is called as an “object method”, etc.). This proved to be annoying with an object-oriented style of programming.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function Person() {
// The Person() constructor defines `this` as an instance of itself.
this.age = 0;
setInterval(function growUp() {
// In non-strict mode, the growUp() function defines `this`
// as the global object, which is different from the `this`
// defined by the Person() constructor.
this.age++;
}, 1000);
}
var p = new Person();
//p is always 0

In ECMAScript 3/5, this issue was fixed by assigning the value in this to a variable that could be closed over.

1
2
3
4
5
6
7
8
9
10
11
function Person() {
var self = this; // Some choose `that` instead of `self`.
// Choose one and be consistent.
self.age = 0;
setInterval(function growUp() {
// The callback refers to the `self` variable of which
// the value is the expected object.
self.age++;
}, 1000);
}

Arrow functions capture the this value of the enclosing context, so the following code works as expected.

1
2
3
4
5
6
7
8
9
function Person(){
this.age = 0;
setInterval(() => {
this.age++; // |this| properly refers to the person object
}, 1000);
}
var p = new Person();
1
2
3
4
5
6
7
8
9
10
11
12
function makeCounter(){
var count = 0;
return () => console.log(count++);
}
var counter = makeCounter();
counter();//0
counter();//1
counter();//2
var counter2 = makeCounter();
counter2();//0
counter2();//1
counter2();//2

In javascript, the only thing that is a lexical scope is a function.

logged works with any function, no matter the arity.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function logged (f) {
return function () {
console.log(f.name + ' was called');
return f.apply(this, arguments);
}
}
function inc (x) {
return x + 1;
}
var loggedInc = logged(inc);
var a = loggedInc(5);
function add (x, y) {
return x + y;
}
var loggedAdd = logged(add);
var b = loggedAdd(1, 2);
console.log(a);
console.log(b);
//inc was called
//add was called
//6
//3

Here is what is looks like when you declare a subclass:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Point {
constructor(x, y){
this.x = x;
this.y = y;
}
toString(){
return '(' + this.x + ',' + this.y +')';
}
}
class Point3D extends Point {
constructor(x, y, y){
super(x, y);
this.z = z;
}
toString(){
return '(' + this.x + ',' + this.y + ',' + this.y + ')';
}
}

Here is its desurgaring

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function Point3D (x, y, z) {
//Call Point's constructor with `this` bound to the new Point3D
Point.call(this, x, y);
//finish initializing the new instance
this.z = z;
}
//way it will inherits all of the methods of the superclass
Point3D.prototype = Object.create(Point.prototype);
Points3D.prototype.toString = function () {
return '(' + this.x + ',' + this.y + ',' + this.z +')';
};

Number factorial

1
2
3
Number.prototype.fact = function(){
return this == 0 ? 1:this*(this - 1).fact();
}

unboxing and boxing(wierd)

1
2
3
4
5
6
new Number(5) instanceof Number
//true
5 instanceof Number
//false
(5).fact() instanceof Object
false
文章目录
  1. 1. Arrow Functions