Open lhlGitHub opened 2 years ago
function _new(target, ...args) {
const obj = Object.create(target.prototype);
const ret = target.apply(obj, args);
return typeof ret === 'object' ? ret : obj;
}
new运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象
举个例子
// Otaku 御宅族,简称宅
function Otaku(name, age) {
this.name = name
this.age = age
}
// 重量
Otaku.prototype.weight = 80
Otaku.prototype.sayHi = function () {
console.log(`I am ${this.name},age is ${this.age}.`);
}
let person = new Otaku("张三", 12)
console.log(person.name);//张三
console.log(person.age);//12
console.log(person.weight);//80
person.sayHi();// I am 张三,age is 12.
从这个例子中,我们可以看到person实例可以:
注意:加入构造函数有返回值,并且返回值是
对象
,则new操作符会返回该对象
// Otaku 御宅族,简称宅
function Otaku(name, age) {
this.name = name
this.age = age
return{
name:"哈哈",
age:22
}
}
// 重量
Otaku.prototype.weight = 80
Otaku.prototype.sayHi = function () {
console.log(`I am ${this.name},age is ${this.age}.`);
}
let person = new Otaku("张三", 12)
console.log(person.name);//哈哈
console.log(person.age);//22
console.log(person.weight);//undefined
person.sayHi();//报错
function objectFactory() {
let obj = new Object()
Constructor = Array.prototype.shift.call(arguments)// 将构造函数取出,arguments剩余的数据为传入的参数
// 将原型指向obj
obj.__proto__ = Constructor.prototype
let result = Constructor.apply(obj, arguments)
// 如果构造函数的返回值不为对象,则返回obj。否则返回该对象result
return Object.prototype.toString.apply(result).includes("Object") ? result : obj
}
测试
// Otaku 御宅族,简称宅
function Otaku(name, age) {
this.name = name
this.age = age
}
// 重量
Otaku.prototype.weight = 80
Otaku.prototype.sayHi = function () {
console.log(`I am ${this.name},age is ${this.age}.`);
}
// let person = new Otaku("张三", 12)
let person = objectFactory(Otaku, "张三", 12)
console.log(person.name);//张三
console.log(person.age);//12
console.log(person.weight);//80
person.sayHi();//I am 张三,age is 12.
参考链接
1-1.使用场景
当需要创建多个对象的时候,如果循环会「重复创建」很多变量,占用内存。
如果用new生成,那么里面重复的属性是在「原型」上,就不用占用内存。
1-2.意义
节省代码,属于语法糖,可以拥有使用构造函数里面的所有「属性和方法」,并且还可以「拓展」。
1-3.实现步骤
1-4.代码实现
新建构造函数--用来new的函数
手写new函数
验证