Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js): use Symbol in Function.prototype.myCall #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions JS/JS-br.md
Original file line number Diff line number Diff line change
Expand Up @@ -752,15 +752,16 @@ Consideramos implementar eles a partir das seguintes regras:
```js
Function.prototype.myCall = function (context) {
var context = context || window
var fn = Symbol('fn')
// Adiciona uma propriedade ao `context`
// getValue.call(a, 'yck', '24') => a.fn = getValue
context.fn = this
context[fn] = this
// pega os parâmentros do `context`
var args = [...arguments].slice(1)
// getValue.call(a, 'yck', '24') => a.fn('yck', '24')
var result = context.fn(...args)
var result = context[fn](...args)
// deleta fn
delete context.fn
delete context[fn]
return result
}
```
Expand All @@ -770,18 +771,19 @@ O exemplo acima é a idéia central da simulação do `call`, e a implementaçã
```js
Function.prototype.myApply = function (context) {
var context = context || window
context.fn = this
var fn = Symbol('fn')
context[fn] = this

var result
// Existe a necessidade de determinar se guarda o segundo parâmentro
// Se o segundo parâmetro existir, espalhe ele
if (arguments[1]) {
result = context.fn(...arguments[1])
result = context[fn](...arguments[1])
} else {
result = context.fn()
result = context[fn]()
}

delete context.fn
delete context[fn]
return result
}
```
Expand Down
16 changes: 9 additions & 7 deletions JS/JS-ch.md
Original file line number Diff line number Diff line change
Expand Up @@ -1018,15 +1018,16 @@ getValue.apply(a, ['yck', '24'])
```js
Function.prototype.myCall = function (context) {
var context = context || window
var fn = Symbol('fn')
// 给 context 添加一个属性
// getValue.call(a, 'yck', '24') => a.fn = getValue
context.fn = this
context[fn] = this
// 将 context 后面的参数取出来
var args = [...arguments].slice(1)
// getValue.call(a, 'yck', '24') => a.fn('yck', '24')
var result = context.fn(...args)
var result = context[fn](...args)
// 删除 fn
delete context.fn
delete context[fn]
return result
}
```
Expand All @@ -1036,18 +1037,19 @@ Function.prototype.myCall = function (context) {
```js
Function.prototype.myApply = function (context) {
var context = context || window
context.fn = this
var fn = Symbol('fn')
context[fn] = this

var result
// 需要判断是否存储第二个参数
// 如果存在,就将第二个参数展开
if (arguments[1]) {
result = context.fn(...arguments[1])
result = context[fn](...arguments[1])
} else {
result = context.fn()
result = context[fn]()
}

delete context.fn
delete context[fn]
return result
}
```
Expand Down
16 changes: 9 additions & 7 deletions JS/JS-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -751,15 +751,16 @@ We can consider how to implement them from the following rules:
```js
Function.prototype.myCall = function (context) {
var context = context || window
var fn = Symbol('fn')
// Add an property to the `context`
// getValue.call(a, 'yck', '24') => a.fn = getValue
context.fn = this
context[fn] = this
// take out the rest parameters of `context`
var args = [...arguments].slice(1)
// getValue.call(a, 'yck', '24') => a.fn('yck', '24')
var result = context.fn(...args)
var result = context[fn](...args)
// delete fn
delete context.fn
delete context[fn]
return result
}
```
Expand All @@ -769,18 +770,19 @@ The above is the main idea of simulating `call`, and the implementation of `ap
```js
Function.prototype.myApply = function (context) {
var context = context || window
context.fn = this
var fn = Symbol('fn')
context[fn] = this

var result
// There's a need to determine whether to store the second parameter
// If the second parameter exists, spread it
if (arguments[1]) {
result = context.fn(...arguments[1])
result = context[fn](...arguments[1])
} else {
result = context.fn()
result = context[fn]()
}

delete context.fn
delete context[fn]
return result
}
```
Expand Down