js function,prototype,sub.
Ojbect 和Function 与普通函数和实例对象
1.实例对象的proto 指向构造函数的原型对象
2.实例对象的proto 指向Ojbect的原型
3.所有函数的proto 都指向Function的原型
function定义的对象有一个prototype属性,使用new生成的对象就没有这个prototype属性。
prototype属性又指向了一个prototype对象,注意prototype属性与prototype对象是两个不同的东西,要注意区别。在prototype对象中又有一个constructor属性,这个constructor属性同样指向一个constructor对象,而这个constructor对象恰恰就是这个function函数本身。
有点头晕,看下图吧:

我们接着看代码:
代码 function Person(name)
{
this.name=name;
this.showMe=function()
{
alert(this.name);
}
};
Person.prototype.from=function()
{
alert('I come from prototype.');
}
var one=new Person('js');
one.showMe();//js,这个结果没有什么好奇怪的
one.from();//I come from prototype.,这个结果有一点奇怪吧
要解释这个结果就要仔细研究一下new这个操作符了.var one=new Person('js');这个语句执行的过程可以分成下面的语句:
var one={};
Person.call(one,'js');
按照《悟透javascript》书中说的,new形式创建对象的过程实际上可以分为三步:
第一步是建立一个新对象(叫A吧);
第二步将该对象(A)内置的原型对象设置为构造函数(就是Person)prototype 属性引用的那个原型对象;
第三步就是将该对象(A)作为this 参数调用构造函数(就是Person),完成成员设置等初始化工作。
其中第二步中出现了一个新名词就是内置的原型对象,注意这个新名词跟prototype对象不是一回事,为了区别我叫它inobj,inobj就指向了函数Person的prototype对象。在person的prototype对象中出现的任何属性或者函数都可以在one对象中直接使用,这个就是javascript中的原型继承了。
又头晕了,上图吧!

这样one对象通过内置的原型对象inobj就可以直接访问Person的prototype对象中的任何属性与方法了。这也就解释了上面的代码中为什么one可以访问form函数了。因为prototype对象中有一个constructor属性,那么one也可以直接访问constructor属性。
__proto__ is inobj????
继承是如何实现的。
代码 function Person(name)
{
this.name=name;
this.showMe=function()
{
alert(this.name);
}
};
Person.prototype.from=function()
{
alert('I come from prototype.');
}
function SubPerson()
{
}
SubPerson.prototype=new Person();
var subOne=new SubPerson();
subOne.from();//I come from prototype.
alert(subOne.constructor);//function Person(name) {...};
alert(SubPerson.prototype.constructor);//function Person(name) {...};
继承的实现很简单,只需要把子类的prototype设置为父类的一个(实例化)对象即可。注意这里说的可是对象哦!
那么通过prototype属性实现继承的原理是什么呢?还是先看图形说明,然后编写代码进行验证。

注意:红色的方框就是把子类与父类链接起来的地方。这个就应该是传说中的prototype链了吧。下面有代码进行验证。
代码 function Person(name)
{
this.name=name;
this.showMe=function()
{
alert(this.name);
}
};
Person.prototype.from=function()
{
alert('I come from prototype.');
}
var father=new Person('js');//为了下面演示使用showMe方法,采用了js参数,实际多采用无参数
alert(father.constructor);//查看构造函数,结果是:function Person(name) {...};
function SubPer()
{
}
SubPer.prototype=father;//注意这里 ////在这里SubPer.prototype就指向了father对应的对象
SubPer.prototype.constructor=SubPer; ////
//这行修改了SubPer.prototype.constructor,其实也给father对象添加了一个constructor属性,此时father.prototype.constructor还是Person,所以father.constructor访问的时候先是找到了自己constructor的属性,就不再去原型里面找
var son=new SubPer();
son.showMe();//js
son.from();//I come from prototype.
alert(father.constructor);//function SubPer(){...}
alert(son.constructor);//function SubPer(){...}
alert(SubPer.prototype.constructor);//function SubPer(){...}
根据上图的prototype链,还有代码的结果,我想应该明白为什么使用prototype能够实现
JS中的继承了吧。
js function,prototype,sub.的更多相关文章
- Js中Prototype、__proto__、Constructor、Object、Function关系介绍
一. Prototype.__proto__与Object.Function关系介绍 Function.Object:都是Js自带的函数对象.prototype,每一个函数对象都有一个显式的proto ...
- prototype.js中Function.prototype.bind方法浅解
prototype.js中的Function.prototype.bind方法: Function.prototype.bind = function() { var __method = this; ...
- 【转】Js中Prototype、__proto__、Constructor、Object、Function关系介绍
一 Prototype.__proto__与Object.Function关系介绍 Function.Object:Js自带的函数对象. prototype,每一个 ...
- js in depth: arrow function & prototype & this & constructor
js in depth: arrow function & prototype & this & constructor https://developer.mozilla.o ...
- js in depth: Object & Function & prototype & __proto__ & constructor & classes inherit
js in depth: Object & Function & prototype & proto & constructor & classes inher ...
- JS魔法堂:再次认识Function.prototype.call
一.前言 大家先预计一下以下四个函数调用的结果吧! var test = function(){ console.log('hello w ...
- js中Object.__proto__===Function.prototype
参考:http://stackoverflow.com/questions/650764/how-does-proto-differ-from-constructor-prototype http:/ ...
- Function.prototype.toString 的使用技巧
Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...
- 关于JS的prototype
在接触JS的过程中,随着理解的深入会逐渐的理解一些比较深奥的理论或者知识,那么今天我们来介绍一下比较难理解的prototype和constructor. 初步理解: 在说prototype和const ...
随机推荐
- git 清除历史
http://stackoverflow.com/questions/9683279/make-the-current-commit-the-only-initial-commit-in-a-git- ...
- Spark-shell 无法启动之网络问题
由于需要首次手动安装sbt,需要联网,故将虚拟机的网络适配器模式设置为"桥接模式",这样就可以和互联网相连接. 但是后面执行"spark-shell --master ...
- Resources.FindObjectsOfTypeAll<T>()的坑(Ghost prefab)
今天遇到了一个Bug,因为调用Resources.FindObjectsOfTypeAll<T>()遍历整个场景,结果遍历出的对象不对.比较哈希一查果然是两个.原来prefab本身和pre ...
- 折腾gcc/g++链接时.o文件及库的顺序问题(转)
转自: http://www.cnblogs.com/OCaml/archive/2012/06/18/2554086.html#sec-1-1 折腾gcc/g++链接时.o文件及库的顺序问题 Tab ...
- ajax回调数据 Structs has detected an unhandled exception 问题
Structs has detected an unhandled exception 今天算倒霉了,用maven写的一个项目,竟然出现了以下低级的错误,在用ajax修改密码时,回调的数据竟然是以下的 ...
- 【问题记录】javaweb项目的jar无法识别注解的bean
问题:eclipse中javaweb项目,打成jar包供其它项目使用,发现无法识别使用注解的bean. 原因参考: http://blog.csdn.net/wangpeng047/article/d ...
- 从浏览器输入URL回车发生了什么
在浏览器输入url后回车,整个过程发生了什么?整个过程如果节节细述的话,那非常的复杂.我就简单的描述一下整个过程 1.查询DNS,获取域名对应的IP地址 (1).浏览器搜索自身的DNS缓存 (2).搜 ...
- JS对象序列化为JSON对象的方法
var $ = $ || {}; /** * 将JS对象序列化为JSON字符串 * @param {Mixed} o The variable to decode * @return {String} ...
- yii2 RESTful API 405 Method Not Allowed
关于 Yii2 中 RESTful API 的开发,可以参考另一篇随笔 http://www.cnblogs.com/ganiks/p/yii2-restful-api-dev.html 测试的过程中 ...
- Supervisord进程管理工具
进程管理工具Supervisord Posted on 2014/06/17 by admin Supervisord 上面已经介绍了Go目前是有两种方案来实现他的daemon,但是官方本身还不支持这 ...