-
Notifications
You must be signed in to change notification settings - Fork 634
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
字节:模拟实现 new 操作符 #71
Comments
new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例。new 关键字会进行如下的操作:
function myNew(fn, ...args) {
const obj = {};
obj.__proto__ = fn.prototype;
const res = fn.call(obj, ...args);
return typeof res === 'object' ? res : obj;
} |
function myNew(fn, ...args) {
let obj = Object.create(fn.prototype); // 相当于 obj.__proto__ = fn.prototype
let result = fn.apply(obj, args); // 执行构造方法, 绑定新 this
// 如果构造方法 return 了一个对象,那么就返回该对象,否则返回创建的新对象
return Object.prototype.toString.call(result) === '[object Object]' ? result : obj;
} |
function myNew() {
// 创建一个空对象
let obj = {};
//获取构造函数
let Con = [].shift.call(arguments);
// 设置对象的原型
obj.__proto__ = Con.prototype;
// 绑定this
let result = Con.apply(obj, arguments);
// 判断返回值是不是一个对象,如果是,那么返回这个对象,否则返回新创建的对象
return typeof result === 'object' ? result : obj;
} |
function _new(Fn, ...args) { |
function myNew(fn, ...args) {
let _this = {}
let result = fn.apply(_this, args)
_this.__proto__ = fn.prototype
return typeof (result === 'object' || typeof result === 'function') &&
result != null
? result
: _this
} |
function _new(){
const Constructor = Array.prototype.shift.call(arguments)
const obj = {}
obj.__proto__ = Constructor.prototype
const ret = Constructor.apply(obj,arguments)
return typeof ret === 'object' ? ret : obj
} |
步骤:创建,执行,原型链,判断。
/**
* @file 模拟new
* @author 阿吉
* @returns {object} 新对象
*/
function lxhNew(){
// 1. 创建
let obj = new Object();
// 2.执行
let Constructor = [].shift.call(arguments);
let result = Constructor.apply(obj, arguments);
// 3.原型链
result.__proto__ = Constructor.prototype;
// 4.判断
return result instanceof Object? result: obj;
} |
|
@sisterAn 最后优化的版本 变量 Con的声明漏掉了let关键词 function create() {
....
let Con = [].shift.call(arguments);
....
}; |
Con.apply(obj, arguments); 这一步是什么意思呀? |
/**
* new 使用Js原生实现
*/
function Parent(name, age) {
this.name = name;
this.age = age;
this.sayName = function () {
console.log(this.name);
}
}
const _new = function (Parent, ...rest) {
//1.以构造器Parent的prototype为原型创建新对象
const child = Object.create(Parent.prototype);
//2. 将this和调用参数传给构造器执行
const result = Parent.apply(child, rest);
return typeof result === 'object' ? result : child;
}
const p1 = _new(Parent,'www','23');
console.log(p1);
p1.sayName(); |
因为 Parent 没有返回值,所以 |
function newFn(f) {
let obj, ret, proto;
proto = Object(f.prototype) === f.prototype ? f.prototype : Object.prototype;
obj = Object.create(proto);
ret = f.apply(obj, Array.prototype.slice.call(arguments, 1));
return Object(ret) === ret ? ret : obj
} |
不相信有人不查阅资料一次能写出来,目前来看最后一步这个写法最科学。 if((type === "object" || type === "function") && res !== null) {
return res;
} 有同学使用 |
new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例。new 关键字会进行如下的操作:
{}
);this
的上下文 ;this
。代码实现:
所以进一步优化 new 实现:
The text was updated successfully, but these errors were encountered: