-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Open
Labels
Description
鉴于十三题可以说的东西很多,就先看了十四题,作为一个单身狗,new的对象都是一个小狗啊
// 实现一个new
var Dog = function(name) {
this.name = name
}
Dog.prototype.bark = function() {
console.log('wangwang')
}
Dog.prototype.sayName = function() {
console.log('my name is ' + this.name)
}
let sanmao = new Dog('三毛')
sanmao.sayName()
sanmao.bark()
// new 的作用
// 创建一个新对象obj
// 把obj的__proto__指向Dog.prototype 实现继承
// 执行构造函数,传递参数,改变this指向 Dog.call(obj, ...args)
// 最后把obj赋值给sanmao
var _new = function() {
let constructor = Array.prototype.shift.call(arguments)
let args = arguments
const obj = new Object()
obj.__proto__ = constructor.prototype
constructor.call(obj, ...args)
return obj
}
var simao = _new(Dog, 'simao')
simao.bark()
simao.sayName()
console.log(simao instanceof Dog) // true
machangzhi, atheist1, zwmmm, ChenZongHeng, yishuihan-001 and 50 moreqq635030106, NuoHui, ZhongQW, tingtingtingsss, dongyuanxin and 26 moreydydydq, AndyZjy, corzero, loriswang, WsilenceY and 5 moreLoonging, songsongQAQ and WsilenceYWsilenceY and SuperDravenduandabiao, chentianyuan, aatoe, XHFkindergarten, WsilenceY and 2 moreWsilenceY, caojikeai and daiwiyNathanHan1, aatoe, hbxtben, corzero, yelin2016 and 8 more
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
zwmmm commentedon Feb 14, 2019
这样写是不是简单点啊
atheist1 commentedon Feb 14, 2019
@zwmmm 你是对的,我写的有点问题
hwxy commentedon Feb 15, 2019
function _new(fn, ...arg) {
var obj = Object.create(fn.prototype);
fn.call(obj, ...arg);
let result = fn();
return Object.prototype.toString.call(result) == '[object Object]' ? result : obj;
}
nnecec commentedon Feb 18, 2019
chenjigeng commentedon Feb 18, 2019
ruochuan12 commentedon Feb 18, 2019
之前写过一篇模拟实现
new
的文章~面试官问:能否模拟实现
JS
的new
操作符我的这篇文章主要提出了对返回值是函数和对象的处理,还有对
new.target
的简单处理。GuoYuFu123 commentedon Feb 19, 2019
我也贴一个
function Dog(name) {
this.name = name
this.say = function () {
console.log('name = ' + this.name)
}
}
function Cat(name) {
this.name = name
this.say = function () {
console.log('name = ' + this.name)
}
}
function _new(fn, ...arg) {
const obj = {};
obj.proto = fn.prototype;
fn.apply(obj, arg)
return Object.prototype.toString.call(obj) == '[object Object]'? obj : {}
}
var dog = _new(Dog,'xxx')
dog.say() //'name = xxx'
console.log(dog instanceof Dog) //true
var cat = _new(Cat, 'carname');
cat.say() //'name = carname'
console.log(cat instanceof Cat) //true
flysme commentedon Feb 19, 2019
写的真好
lkoma commentedon Feb 21, 2019
这样ret不是一直都是undefined吗?res instanceof Object就一直是false啊
atheist1 commentedon Feb 23, 2019
当然不是,这里判断构造函数执行的返回类型,如果构造函数执行结果返回的是个对象不就是true了
zwmmm commentedon Feb 25, 2019
构造函数是默认返回对象的(但是这个是没有返回值的)。但是你也可以自己return一个对象。
Hiker9527 commentedon Apr 2, 2019
先理清楚 new 关键字调用函数都的具体过程,那么写出来就很清楚了
cliYao commentedon Apr 17, 2019
function _new(fn , ...args){
const obj={}
const Constructor = fn
obj.proto = Constructor.prototype
const result = Constructor.call(obj , ...args)
return typeof result === "object" ? result : obj
}
[-]14题 情人节快乐![/-][+]第 14 题:情人节福利题,如何实现一个 new[/+]44 remaining items