Javascript中的Prototype到底是什么


Posted in Javascript onFebruary 16, 2016

Javascript也是面向对象的语言,但它是一种基于原型Prototype的语言,而不是基于类的语言。在Javascript中,类和对象看起来没有太多的区别。

什么是prototype:

function定义的对象有一个prototype属性,prototype属性又指向了一个prototype对象,注意prototype属性与prototype对象是两个不同的东西,要注意区别。在prototype对象中又有一个constructor属性,这个constructor属性同样指向一个constructor对象,而这个constructor对象恰恰就是这个function函数本身。 是不是很绕?用伪代码表示如下:

var function{
prototype:prototype{
constructor:constructor == function
}
}

还不明白?看图吧:

Javascript中的Prototype到底是什么

prototype的作用:

这个prototype到底有什么作用呢?看下面的例子:

function 3water(){
}
3water.prototype.name = "a";
var test = new 3water();
alert(test.name)//"a";

奇怪吧,明明没有为test设置name属性,可是为什么会有值?

这就是prototype的功劳了,uw3c中prototype属性中的name对象,在uw3c被new构造函数之后,被继承到了对象test的属性中。接着看:

var name = "js";
function 3water(name){
alert(this.name);//"css"
}
3water.prototype.name = "css";
var test = new 3water();
test()

为什么alert的值不是“js”?这个过程大致如下:

var test={};
uw3c.call(test);

第一步是建立一个新对象(test)。

第二步将该对象(test)内置的原型对象设置为构造函数(就是uw3c)prototype 属性引用的那个原型对象。

第三步就是将该对象(test)作为this 参数调用构造函数(就是uw3c),完成成员设置等初始化工作。

其中第二步中出现了一个新名词就是内置的原型对象,注意这个新名词跟prototype对象不是一回事, 为了区别我叫它inobj,inobj就指向了函数uw3c的prototype对象。在uw3c的prototype对象中出现的任何属性或者函数都可以在test对象中直接使用,这个就是JS中的原型继承了。

通常,这样创建一个对象:

function person(name){
this.sayHi = function(){
alert('hi ' + this.name);
}
this.name = name;
}
var p = new person("dan");
p.sayHi();

以上,使用new关键字,通过对象(函数也是特殊对象)创建一个对象实例。

在基于类的语言中,属性或字段通常都是在类中事先定义好了,但在Javascript中,在创建对象之后还可以为类添加字段。

function animal(){}
var cat = new animal();
cat.color = "green";

以上,color这个字段只属于当前的cat实例。
对于后加的字段,如果想让animal的所有实例都拥有呢?

--使用Prototype
function animal(){}
animal.prototype.color= "green";
var cat = new animal();
var dog = new animal();
console.log(cat.color);//green
console.log(dog.color);//green

通过Prototype不仅可以添加字段,还可以添加方法。

function animal(){}
animal.prototype.color= "green";
var cat = new animal();
var dog = new animal();
console.log(cat.color);//green
console.log(dog.color);//green
animal.prototype.run = funciton(){
console.log("run");
}
dog.run();

原来通过prototype属性,在创建对象之后还可以改变对象的行为。
比如,可以为数组这个特殊对象添加一个方法。

Array.prototype.remove = function(elem){
var index = this.indexof(elem);
if(index >= 0){
this.splice(index, 1);
}
}
var arr = [1, 2, 3] ;
arr.remove(2);

除了通过prototype为对象定义属性或方法,还可以通过对象的构造函数来定义类的属性或方法。

function animal(){
this.color = "green";
this.run = function(){
console.log("run");
}
}
var mouse = new animal();
mouse.run();

以上做法的也可以让所有的animal实例共享所有的字段和方法。并且还有一个好处是可以在构造函数中使用类的局部变量。

function animal(){
var runAlready = false;
this.color = "green";
this.run = funciton(){
if(!runAlreadh){
console.log("start running");
} else {
console.log("already running")
}
}
}

其实,一个更加实际的做法是把通过构造函数结合通过prototype定义一个类的字段和行为。

function animal(){
var runAlready = false;
this.run = function(){
if(!runAlready){
console.log('i am running');
} else {
console.log("i am already running");
}
}
}
animal.prototype.color = '';
animal.prototype.hide = funciton(){
console.log("");
}
var horse = new animal();
horse.run();
horse.hide();

Prototype允许我们在创建对象之后来改变对象或类的行为,并且这些通过prototype属性添加的字段或方法所有对象实例是共享的。

Javascript 相关文章推荐
通过修改referer下载文件的方法
May 11 Javascript
javascript 数组排序函数
Aug 20 Javascript
js实现屏蔽默认快捷键调用自定义事件示例
Jun 18 Javascript
JS冒泡事件的快速解决方法
Dec 16 Javascript
thinkphp中常用的系统常量和系统变量
Mar 05 Javascript
jquery重复提交请求的原因浅析
May 23 Javascript
使用pjax实现无刷新更改页面url
Feb 05 Javascript
JQuery实现带排序功能的权限选择实例
May 18 Javascript
AngularJS入门教程之更多模板详解
Aug 19 Javascript
vue-cli + sass 的正确打开方式图文详解
Oct 27 Javascript
vue嵌套路由与404重定向实现方法分析
May 04 Javascript
js实现滑动进度条效果
Aug 21 Javascript
剖析Node.js异步编程中的回调与代码设计模式
Feb 16 #Javascript
使用Node.js处理前端代码文件的编码问题
Feb 16 #Javascript
让图片跳跃起来  javascript图片轮播特效
Feb 16 #Javascript
Node.js本地文件操作之文件拷贝与目录遍历的方法
Feb 16 #Javascript
详解Node.js包的工程目录与NPM包管理器的使用
Feb 16 #Javascript
javascript每日必学之运算符
Feb 16 #Javascript
解析Node.js基于模块和包的代码部署方式
Feb 16 #Javascript
You might like
编写PHP的安全策略
2006/10/09 PHP
PHP MYSQL乱码问题,使用SET NAMES utf8校正
2009/11/30 PHP
PHP新手入门学习方法
2011/05/08 PHP
php实现aes加密类分享
2014/02/16 PHP
Laravel框架基础语法与知识点整理【模板变量、输出、include引入子视图等】
2019/12/03 PHP
用Laravel轻松处理千万级数据的方法实现
2020/12/25 PHP
IE和Firefox下event事件杂谈
2009/12/18 Javascript
DIV外区域Click后关闭DIV的实现代码
2011/12/21 Javascript
javascript学习笔记(八) js内置对象
2012/06/19 Javascript
Jquery通过Ajax方式来提交Form表单的具体实现
2013/11/07 Javascript
使用Raygun对Node.js应用进行错误处理的方法
2015/06/23 Javascript
浅析JavaScript访问对象属性和方法及区别
2015/11/16 Javascript
JavaScript jquery及AJAX小结
2016/01/24 Javascript
jQuery文字提示与图片提示效果实现方法
2016/07/04 Javascript
vue2 自定义动态组件所遇到的问题
2017/06/08 Javascript
AngularJS select加载数据选中默认值的方法
2018/02/28 Javascript
vue element-ui table表格滚动加载方法
2018/03/02 Javascript
详解react、redux、react-redux之间的关系
2018/04/11 Javascript
讲解vue-router之什么是编程式路由
2018/05/28 Javascript
详解element-ui 表单校验 Rules 配置 常用黑科技
2020/07/11 Javascript
[49:17]DOTA2-DPC中国联赛 正赛 Phoenix vs Dynasty BO3 第三场 1月26日
2021/03/11 DOTA
python使用range函数计算一组数和的方法
2015/05/07 Python
python去掉行尾的换行符方法
2017/01/04 Python
Django之Mode的外键自关联和引用未定义的Model方法
2018/12/15 Python
利用ImageAI库只需几行python代码实现目标检测
2019/08/09 Python
Python 中判断列表是否为空的方法
2019/11/24 Python
Python实现大数据收集至excel的思路详解
2020/01/03 Python
详解Python IO编程
2020/07/24 Python
python 通过pip freeze、dowload打离线包及自动安装的过程详解(适用于保密的离线环境
2020/12/14 Python
TUMI澳大利亚网站:美国旅行箱包品牌
2017/03/27 全球购物
英文简历自荐信范文
2013/12/11 职场文书
人事文员岗位职责
2014/02/16 职场文书
考博专家推荐信
2014/05/10 职场文书
主题团日活动总结
2014/06/25 职场文书
2015年仓库工作总结
2015/04/09 职场文书
小学主题班会教案
2015/08/17 职场文书