//实现一个new
var Dog=function(name){
this.name=name
}
Dog.prototype.wang=function(){
console.log("汪汪汪")
}
Dog.prototype.sayName=function(){
console.log("My name is"+this.name)
}
//实例化对象
let dongge=new Dog("冬哥");
dongge.sayName();//My name is 冬哥
dongge.wang(); //汪汪汪
new 的作用
创建一个新对象obj
把obj的 _ proto _ 指向Dog.prototype 实现继承
执行构造函数,传递参数,改变this指向 Dog.call(obj, ...args)
最后把obj赋值给dongge
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 dongge = _new(Dog, 'dongge')
dongge.bark()
dongge.sayName()
console.log(dongge instanceof Dog) // true