From 4d663acd421b19b9559cef51bc2d9c862553bbb8 Mon Sep 17 00:00:00 2001 From: liqing Date: Tue, 29 Jan 2019 11:54:17 +0800 Subject: [PATCH] fix(js): use Symbol in Function.prototype.myCall --- JS/JS-br.md | 16 +++++++++------- JS/JS-ch.md | 16 +++++++++------- JS/JS-en.md | 16 +++++++++------- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/JS/JS-br.md b/JS/JS-br.md index 877fd3d1..218e8f6c 100644 --- a/JS/JS-br.md +++ b/JS/JS-br.md @@ -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 } ``` @@ -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 } ``` diff --git a/JS/JS-ch.md b/JS/JS-ch.md index e2ac2e23..55d37299 100644 --- a/JS/JS-ch.md +++ b/JS/JS-ch.md @@ -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 } ``` @@ -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 } ``` diff --git a/JS/JS-en.md b/JS/JS-en.md index e0191761..f338a279 100644 --- a/JS/JS-en.md +++ b/JS/JS-en.md @@ -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 } ``` @@ -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 } ```