![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
原型原型链继承
Fx_cap
前端
展开
-
js实现继承的几种方式
原型链继承 // 原型链的继承function Person() { this.hobby = { sing: '小星星' }}function Man() {}Man.prototype = new Person()Man.prototype.constructor = Manconst man1= new Man()const man2 = new Man()man2.hobby.sing = '两只老虎'console.log(man1.原创 2022-03-07 17:41:09 · 57 阅读 · 0 评论 -
new都做了什么,实现一个new
首先创建一个新的空对象 根据原型链,设置空对象的__proto__为构造函数的prototype 构造函数的this指向这个对象,并执行构造函数的代码(为这个新对象添加属性) 判断函数的返回值类型,如果是引用类型,就返回这个引用类型的对象function myNew(context) { const obj = new Object(); obj.__proto__ = context.prototype; const res = context.apply(obj, [.原创 2022-02-16 11:02:34 · 162 阅读 · 0 评论 -
ES6 class 类 继承
概念:讲对象中公共的属性或方法封装成一个模版(ES5中构造函数) 作用:创建对象 语法://使用class关键字,Person就是一大类名,可以理解为构造函数的名称class Person {}// 使用new关键字创建实例const xx = new Person()类里面添加属性://使用class关键字,Person就是一大类名,可以理解为构造函数的名称class Star { // 类的公共属性放在 constructor 内 constructor(na原创 2022-01-11 11:22:09 · 169 阅读 · 0 评论 -
手写 call apply bind
// 定义一个 show 方法 function show(...args){ console.log(this); } // 原型上新增一个 myCall 方法 Function.prototype.myCall=function(ctx,...args){ // console.log(this);//此时 this 指向 show 方法 ctx.fn=t...原创 2021-11-18 15:11:27 · 300 阅读 · 0 评论 -
call apply bind 的使用方法
function Father(name) { this.name = name console.log(this); } const obj={ age:12 } Father.call(obj, 'zs') Father.apply(obj, ['zs']) Father.bind(obj, 'zs')()原创 2021-11-19 11:08:39 · 159 阅读 · 0 评论 -
原型及原型链
原型对象 protoypeprototype:叫原型对象,是每一个构造函数都有的一个属性,该属性是以对象的形式存在的(原型对象)function Person(uname) { this.uname = uname; this.sing = function () { }}// Person的实例化对象const obj1 = new Person("zs");const obj2 = new Person("ls");// 两个实例化对象的sing方法不是同一内存地..原创 2022-01-10 14:41:22 · 331 阅读 · 0 评论 -
ES5的继承
function Other(house) { this.house = house}Other.prototype.sing = function () { console.log('sing');}function My(house) { Other.call(this, house)}const mine = new My('我的房子')const other = new Other('他的房子')// console.log(My.prototype ...原创 2022-01-10 16:17:14 · 191 阅读 · 0 评论