转自:微点阅读  https://www.weidianyuedu.com

JS中的phototype是JS中比较难理解的一个部分

本文基于下面几个知识点:

1 原型法设计模式

在.Net中可以使用clone()来实现原型法

原型法的主要思想是,现在有1个类A,我想要创建一个类B,这个类是以A为原型的,并且能进行扩展。我们称B的原型为A。

2 javascript的方法可以分为三类:

a 类方法

b 对象方法

c 原型方法

例子:

function People(name)

{

this.name=name;

//对象方法

this.Introduce=function(){

alert("My name is "+this.name);

}

}

//类方法

People.Run=function(){

alert("I can run");

}

//原型方法

People.prototype.IntroduceChinese=function(){

alert("我的名字是"+this.name);

}

//测试

var p1=new People("Windking");

p1.Introduce();

People.Run();

p1.IntroduceChinese();

3 obj1.func.call(obj)方法

意思是将obj看成obj1,调用func方法

好了,下面一个一个问题解决:

prototype是什么含义?

javascript中的每个对象都有prototype属性,Javascript中对象的prototype属性的解释是:返回对象类型原型的引用。

A.prototype = new B();

理解prototype不应把它和继承混淆。A的prototype为B的一个实例,可以理解A将B中的方法和属性全部克隆了一遍。A能使用B的方法和属性。这里强调的是克隆而不是继承。可以出现这种情况:A的prototype是B的实例,同时B的prototype也是A的实例。

先看一个实验的例子:

function baseClass()

{

this.showMsg = function()

{

alert("baseClass::showMsg");

}

}

function extendClass()

{

}

extendClass.prototype = new baseClass();

var instance = new extendClass();

instance.showMsg(); // 显示baseClass::showMsg

我们首先定义了baseClass类,然后我们要定义extentClass,但是我们打算以baseClass的一个实例为原型,来克隆的extendClass也同时包含showMsg这个对象方法。

extendClass.prototype = new baseClass()就可以阅读为:extendClass是以baseClass的一个实例为原型克隆创建的。

那么就会有一个问题,如果extendClass中本身包含有一个与baseClass的方法同名的方法会怎么样?

下面是扩展实验2:

function baseClass()

{

this.showMsg = function()

{

alert("baseClass::showMsg");

}

}

function extendClass()

{

this.showMsg =function ()

{

alert("extendClass::showMsg");

}

}

extendClass.prototype = new baseClass();

var instance = new extendClass();

instance.showMsg();//显示extendClass::showMsg

实验证明:函数运行时会先去本体的函数中去找,如果找到则运行,找不到则去prototype中寻找函数。或者可以理解为prototype不会克隆同名函数。

那么又会有一个新的问题:

如果我想使用extendClass的一个实例instance调用baseClass的对象方法showMsg怎么办?

答案是可以使用call:

extendClass.prototype = new baseClass();

var instance = new extendClass();

var baseinstance = new baseClass();

baseinstance.showMsg.call(instance);//显示baseClass::showMsg

这里的baseinstance.showMsg.call(instance);阅读为“将instance当做baseinstance来调用,调用它的对象方法showMsg”

好了,这里可能有人会问,为什么不用baseClass.showMsg.call(instance);

这就是对象方法和类方法的区别,我们想调用的是baseClass的对象方法

最后,下面这个代码如果理解清晰,那么这篇文章说的就已经理解了:

<script type="text/javascript">

function baseClass()

{

this.showMsg = function()

{

alert("baseClass::showMsg");

}

this.baseShowMsg = function()

{

alert("baseClass::baseShowMsg");

}

}

baseClass.showMsg = function()

{

alert("baseClass::showMsg static");

}

function extendClass()

{

this.showMsg =function ()

{

alert("extendClass::showMsg");

}

}

extendClass.showMsg = function()

{

alert("extendClass::showMsg static")

}

extendClass.prototype = new baseClass();

var instance = new extendClass();

instance.showMsg(); //显示extendClass::showMsg

instance.baseShowMsg(); //显示baseClass::baseShowMsg

instance.showMsg(); //显示extendClass::showMsg

baseClass.showMsg.call(instance);//显示baseClass::showMsg static

var baseinstance = new baseClass();

baseinstance.showMsg.call(instance);//显示baseClass::showMsg

</script>

关于JS中的prototype介绍相关推荐

  1. JS中对于prototype的理解

    JS中的prototype是JS中比较难理解的一个部分 本文基于下面几个知识点: 1 原型法设计模式 在.Net中可以使用clone()来实现原型法 原型法的主要思想是,现在有1个类A,我想要创建一个 ...

  2. 帮你彻底搞懂JS中的prototype、__proto__与constructor(图解)

    帮你彻底搞懂JS中的prototype.__proto__与constructor(图解) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文 ...

  3. 这一篇彻底搞懂JS中的prototype、__proto__与constructor真的很好

    文章目录 1. 前言 2. _ _ proto _ _ 属性 3. prototype属性 4. constructor属性 5. 总结 提示:不要排斥,静下心来,认真读完,你就搞懂了!(可以先看一下 ...

  4. JS中的prototype、__proto__与constructor

    作为一名前端工程师,必须搞懂JS中的prototype.__proto__与constructor属性,相信很多初学者对这些属性存在许多困惑,容易把它们混淆,本文旨在帮助大家理清它们之间的关系并彻底搞 ...

  5. JS中的prototype

    JS中的prototype 2011-06-03 14:40 by 轩脉刃, 17040 阅读, 16 评论, 收藏, 编辑 JS中的phototype是JS中比较难理解的一个部分 本文基于下面几个知 ...

  6. js中的prototype的理解

    2019独角兽企业重金招聘Python工程师标准>>> JS中的prototype是JS中比较难理解的一个部分 1 原型法设计模式 在.Net中可以使用clone()来实现原型法 原 ...

  7. JS中的prototype、__proto__与constructor(图解)

    作为一名前端工程师,必须搞懂JS中的prototype.__proto__与constructor属性,相信很多初学者对这些属性存在许多困惑,容易把它们混淆,本文旨在帮助大家理清它们之间的关系并彻底搞 ...

  8. (转)帮你彻底搞懂JS中的prototype、__proto__与constructor(图解)

    文章目录 1. 前言 2. _ _ proto _ _ 属性 3. prototype属性 4. constructor属性 5. 总结 提示:不要排斥,静下心来,认真读完,你就搞懂了!(可以先看一下 ...

  9. JS中的prototype、__proto__与constructor,原型和原型链

    理解原型的几个关键点: 1.所有的引用类型(数组.函数.对象)可以自由扩展属性(除null以外); 2.所有的引用类型(对象)都有一个'_ _ proto_ _'属性(也叫隐式原型,它是一个普通的对象 ...

最新文章

  1. 双十二爬虫顶流崔庆才老师来图灵直播啦!快来围观啊!!!
  2. 以未来的计算机为题写一篇作文,请以“未来的交通工具”为题写一篇英语作文...
  3. Mysql迁移到Postgresql
  4. solidworks2018安装教程
  5. C# 搭建自己的NuGet服务器,上传自定义NuGet包
  6. 主成分分析步骤_多元分析(1)--主成分分析
  7. python tcp服务器并发_python tcp并发服务器
  8. Golang笔记—封装/继承/接口
  9. 蓝桥杯 ALGO-68 算法训练 判定数字
  10. 安装Caffe报错:/usr/local/cuda-10.1/targets/x86_64-linux/lib/libcudnn.so.7 is not a symbolic link
  11. python while true循环_python学习——while True的用法
  12. ubuntu mysql卸载教程_ubuntu下安装mysql及卸载mysql详细教程/方法
  13. 硬件管理 远程开机,网络开机
  14. Vue - 每个页面单独设置 body 背景色(独立修改单个页面的背景色,不同页面设置不同的背景颜色)
  15. canvas中文显示乱码 html5_如何使用HTML5 canvas绘制文字
  16. 百度网盘网页端的视频如何调节播放倍速?
  17. scp或者ssh报错“no matching host key type found. Their offer: ssh-rsa,ssh-dss“
  18. 学习区块链要掌握哪些专项能力?区块链学习培训多长时间?
  19. 《Java解惑》系列——02字符谜题——谜题17:嗯??
  20. Python:whl文件简介及实践

热门文章

  1. 汇编语言入门·打印输出“Hello,Assembly”
  2. 硬盘图片丢失怎么办?看看这常见的三种恢复方法
  3. 单片机小白篇1 -51单片机代码加密程序
  4. MacBook Pro笔记本硬盘坏了
  5. 试用码云gitee上开源项目“小威架构 / boot-backend“的代码生成器遇到的坑
  6. 尝试实现一个简单的Promise
  7. Macos上一款专业的文字处理器
  8. html语言如何修改字体粗细,如何使用JavaScript更改文本的字体粗细
  9. 计算机网络全部实验,计算机网络实验(全).doc
  10. 钉钉小程序sjs日期对象