▲ 点击上方蓝字关注我 ▲
文 / 景朝霞
来源公号 / 朝霞的光影笔记
ID / zhaoxiajingjing
目录:0 / 原型重定向1 / Object.assign 浅合并2 / 向内置类的原型扩展方法
0 / 原型重定向
function fun(){
this.a=0; this.b=function(){
alert(this.a); }}fun.prototype={ // 原型重定向 b:function(){
this.a=20; alert(this.a); }, c:function(){
this.a=30; alert(this.a) }}var my_fun=new fun();my_fun.b();my_fun.c();
△ 做个题
△ 图1_原型重定向
在这里的fun.prototye={....}
手动将函数fun
的原型重定向,有利有弊
构造函数的原型重定向,优势:
① 把原型上为其实例提供的公共属性和方法,全部写在一起,提高整体性和模块性
② 向其原型上扩展方法会更容易
③ ……
【没有重定向】需要一个一个添加上去:
fun.prototype.a = function(){....}
fun.prototype.b = function(){....}
【重定