-
Notifications
You must be signed in to change notification settings - Fork 184
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
「评论」前端面试中常考的源码实现 #57
Comments
手动实现 call 的函数中使用了 context 的 fn 属性,如果传入的参数 context 中本来就有 fn 属性且调用函数 test 中使用到 fn 的话怕是会出错。比如 |
关于深拷贝那段,我在网上看到很多下面的代码,但是我不知道哪里有坑,用了楼主的示例结果也没问题。 |
这里确实有问题,输出是:
原因在于手动实现的 修正版: Function.prototype.call2 = function(context) {
if (typeof this !== "function") {
throw new TypeError("Error");
}
context = context || window;
const fn = Symbol('fn');
context[fn] = this;
const args = [...arguments].slice(1);
const result = context[fn](...args);
delete context[fn];
return result;
}; |
它的没问题,而且实现的比我的精简多了。不过同样没考虑“循环引用”的问题。
|
|
如果obj[key] = null的话, obj[key] && typeof obj[key] === 'object 为false |
实现call/apply方法还是有一些不足的。给context添加额外属性会造成调用方法时,如果涉及到例如 另外 context = arguments[0] instanceof Object ? arguments[0] : new arguments[0].__proto__.constructor(arguments[0]); 来实现的。 |
使用 ES5 实现的双向绑定有点小问题。功能上完全可以, 但是obj可以直接定义一个空对象。 const obj = {};
// ...
Object.defineProperty(obj, 'value', {
get: () => {
return document.querySelector('input').value;
},
set: (newValue) => {
document.querySelector("#value").innerHTML = newValue;
}
}) |
前端面试中常考的源码实现:https://xin-tan.com/passages/2019-03-18-interview-js-code/
The text was updated successfully, but these errors were encountered: