implement myCall, myApply, myBind
Show the code directly.
myCall:
1 | Function.prototype.myCall = function(ctx) { |
myApply:
1 | Function.prototype.myApply = function(ctx) { |
myBind:
1 | Function.prototype.myBind = function(ctx) { |
Show the code directly.
myCall:
1 | Function.prototype.myCall = function(ctx) { |
myApply:
1 | Function.prototype.myApply = function(ctx) { |
myBind:
1 | Function.prototype.myBind = function(ctx) { |
Last a few days, I just looked through some basic rules about prototype and __proto__. But today when I learnt how to implement the function call() by myself, I found that there’s something unclear.
1 | Function.prototype.myCall = function(ctx) { |
How to understand the third line? What does this refer to?
First, let’s recall how we use the function myCall.
1 | Function.prototype.myCall = function(ctx) { |
Wait. As far as I’m concerned, only Object can has a method. But how could a function use a method?
So I looked through some information, and a picture can conclude it well.
So it’s easy for us to understand:
1 | Function.prototype.myCall = function(ctx) { |
So how do we understand the total process of the code?
First we define a method myCall for Function.prototype, then we define a function a, which is inherited from Function.prototype.
In the last line, we make the function a (also an object) call the method myCall. First it look up in a‘s properties, but cannot match, so it backtracks to a.__proto__ AKA Function.prototype. So it finally finds the method, and this refers to the object & function a.
Finally we revise the basic knowledge of prototype chain.
Object.prototype(top of the prototype chain)
Function.prototype inherited from Object.prototype
Function and Object inherited from Function.prototype.
There are some thoughts on the topic which is interestring.
开始采用暴力法,run time error
1 | var lastRemaining = function(n, m) { |
在JavaScript语言中会爆栈,因此改写为迭代
1 | var lastRemaining = function (n, m) { |
在项目进行搜索跳转优化时,有几点需要注意下。
SEO优化&非必要筛选条件不展示比如sort=,使用的href属性替代
这样在html文件中是可以直接看到该网址的。
选中无法点击,判断item是否selected
采用FilterLink组件而非a标签以及memo方法可以保证在属性不变的情况下不需要重新渲染:
1 | const FilterLink = memo(({name ,query, lang, sort, order}) =>{ |
在展示仓库列表时,可以通过点击仓库的名字进入查看指定仓库的详细信息,而指定仓库的详细信息的头部基本信息展示是与仓库列表中信息的展示重合的,所以使用LRU Cache将仓库基本信息进行缓存,当浏览器端进入详情页时优先加载LRU Cache中的信息。
使用bundle-analyzer进行打包分析,优化moment.js, 将locale下面的语言包仅支持中文
At first use recursion:
1 | var twoSum = function(n) { |
Unfortunetely, rum time error.
Refer to the official answer, we could use iterative method.
1 | var twoSum = function(n) { |
The time complexity is O(n2), and space complexity is O(n).
this means where the current property is from.
this points to the object which is the last to calls it.
1 | var name = "windowsName"; |
1 | var name = "windowsName"; |
1 | var name = "windowsName"; |
1 | var name = "windowsName"; |
this in arrow function points where the function is defined, instead of conducting.
If there is no this in arrow function, we must find the scope chain to find the value. if a non-arrow function contains the arrow function, this in the arrow function is the this in the non-arrow function, otherwise this is undefined.
1 | var name = "windowsName"; |
1 | var name = "windowsName"; |
apply & call difference:
1 | fun.apply(thisArg, [argsArray]) |
1 | var a = { |
How to understand javascript new operation?
A easy example:
1 | function soldier(ID) { |
Using new, we can prevent doing the following things:
After we using new :
1 | function soldier(ID) { |
There is a property named constructor:
1 | soldier.prototype = { |
So we need to define the property seperatedly.
First let’s do a test:
1 | function Test(name) { |
Test if we return a value in the function:
1 | function Test(name) { |
So if we return a value in the function, there is no difference.
Test if we return an object in the function:
1 | function Test(name) { |
So if we return an object, the new doesnt effect.
1 | function create(fun, ...args) { |
Then we test function create :
1 | function Test(name, age) { |
1 | function A() { |
函数A的内部函数B被函数A外的一个变量C所引用
当一个内部函数被其外部函数之外的变量引用时,就形成了一个闭包
1 | function A() { |
在JavaScript中,如果一个对象不再被引用,那么这个对象就会被GC回收,否则一直保存在内存中
B在A中被定义,因此B依赖于A,而外部变量C引用了B,所以A间接被C引用。
1 | (function (document) { |
(function(){})为一个匿名函数,而()执行。
全局对象window引用了obj,而obj依赖于匿名函数,且操作其viewport,故形成闭包。
When it comes to inheritance, there is only one data type in js: object. Every object has a private property named \_proto___ which points to its prototype. The top of the prototype is Object. Object.__proto__ === null
1 | // 让我们从一个函数里创建一个对象o,它自身拥有属性a和b的: |
1 | function doSomething(){} |
1 | { |
1 | function foo() {} |
1 | function foo(){} |
IE8 unsupported
perform badly
need abandoning
prototype
和 Object.getPrototypeOf
1 | var a1 = new A(); |
令牌(token)与密码(password)的作用是一样的,都可以进入系统,但是有三点差异。
(1)令牌是短期的,到期会自动失效,用户自己无法修改。密码一般长期有效,用户不修改,就不会发生变化。
(2)令牌可以被数据所有者撤销,会立即失效。以上例而言,屋主可以随时取消快递员的令牌。密码一般不允许被他人撤销。
(3)令牌有权限范围(scope),比如只能进小区的二号门。对于网络服务来说,只读令牌就比读写令牌更安全。密码一般是完整权限。
上面这些设计,保证了令牌既可以让第三方应用获得权限,同时又随时可控,不会危及系统安全。这就是 OAuth 2.0 的优点。
In config.js
1 | const GITHUB_OAUTH_URL = 'https://github.com/login/oauth/authorize' |
In server/auth.js
1 | // 处理github返回的auth code |