wuyongyu / fe-knowledge

📚积累前端知识
2 stars 0 forks source link

JavaScript函数和React源码 #1

Open wuyongyu opened 6 years ago

wuyongyu commented 6 years ago

// 斐波那契函数

function fibonacci(num){
    if(num === 1 || num === 2){
        return 1;
    }
    return fibonacci(num - 1) + fibonacci(num - 2);
}

===

(0.8 + 0.7 + 0.6 + 0.9) / 4 // 0.75 (0.6 + 0.8 + 0.7 + 0.9) / 4 // 0.7499999999999999

(6 + 8 + 7 + 9) / 4 // 7.5 (8 + 7 + 6 + 9) / 4 // 7.5

在字典中最好使用整数值

wuyongyu commented 6 years ago
wuyongyu commented 6 years ago

比如:

isNaN(NaN); // true
isNaN(undefined); // true
isNaN({}); // true
wuyongyu commented 6 years ago
0.1 + 0.2; // 0.30000000000000004

(0.1 + 0.2) + 0.3; // 0.6000000000000001

0.1 + (0.2 + 0.3); // 0.6

所以平时尽可能地采用整数值运算

wuyongyu commented 6 years ago
wuyongyu commented 6 years ago
this.foo = ::this.foo; 
this.foo = this.foo.bind(this);

【两个冒号的::操作符叫做 bind 操作符】

wuyongyu commented 6 years ago

Erlang 语言的作者 Joe Armstrong 说,面向对象语言的问题是,它们永远都要随身携带那些隐式的环境。你只需要一个香蕉,但却得到一个拿着香蕉的大猩猩...以及整个丛林

wuyongyu commented 5 years ago

React源码中 $$typeof 有什么作用?

wuyongyu commented 5 years ago

React源码中,为什么会用 0xeac7 ?

// The Symbol used to tag the ReactElement type. If there is no native Symbol
// nor polyfill, then a plain number is used for performance.
var TYPE_SYMBOL = (typeof Symbol === 'function' && Symbol.for &&
                  Symbol.for('react.element')) || 0xeac7;
wuyongyu commented 5 years ago

image

wuyongyu commented 5 years ago

image

正确答案: script start async1 start async2 promise1 script end async1 end promise2 undefined setTimeout