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.的更多相关文章

  1. Js中Prototype、__proto__、Constructor、Object、Function关系介绍

    一. Prototype.__proto__与Object.Function关系介绍 Function.Object:都是Js自带的函数对象.prototype,每一个函数对象都有一个显式的proto ...

  2. prototype.js中Function.prototype.bind方法浅解

    prototype.js中的Function.prototype.bind方法: Function.prototype.bind = function() { var __method = this; ...

  3. 【转】Js中Prototype、__proto__、Constructor、Object、Function关系介绍

    一    Prototype.__proto__与Object.Function关系介绍        Function.Object:Js自带的函数对象.         prototype,每一个 ...

  4. js in depth: arrow function & prototype & this & constructor

    js in depth: arrow function & prototype & this & constructor https://developer.mozilla.o ...

  5. js in depth: Object & Function & prototype & __proto__ & constructor & classes inherit

    js in depth: Object & Function & prototype & proto & constructor & classes inher ...

  6. JS魔法堂:再次认识Function.prototype.call

    一.前言                                大家先预计一下以下四个函数调用的结果吧! var test = function(){ console.log('hello w ...

  7. js中Object.__proto__===Function.prototype

    参考:http://stackoverflow.com/questions/650764/how-does-proto-differ-from-constructor-prototype http:/ ...

  8. Function.prototype.toString 的使用技巧

    Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...

  9. 关于JS的prototype

    在接触JS的过程中,随着理解的深入会逐渐的理解一些比较深奥的理论或者知识,那么今天我们来介绍一下比较难理解的prototype和constructor. 初步理解: 在说prototype和const ...

随机推荐

  1. 点滴积累【JS】---JS小功能(JS实现隐藏显示侧边栏,也就是分享栏的隐藏显示)

    效果: 思路: 首先,利用计时器setInterval实现DIV的隐藏显示功能,然后在进行一个判断,之后在把要移动的相应距离进行一个参数传递,再根据参数判断出移动的方向也就是offsetLeft移动的 ...

  2. Atitit.获取某个服务 网络邻居列表 解决方案

    Atitit.获取某个服务 网络邻居列表 解决方案 原理,带入某个ip扫描从0---255 很快,多线程几秒就可以出来. 使用CountDownLatch来join线程.. 返回  [{ " ...

  3. 2.Stacks(堆栈)

    一.概述 C++ Stack(堆栈) 是一个容器类的改编,为程序员提供了堆栈的全部功能,也就是说实现了一个先进后出(FILO)的数据结构. 二.常用API empty() 堆栈为空则返回真 pop() ...

  4. Python随手记

    类属性的本质是变量对象. os.path.abspath(path) 返回绝对路径,如果填入相对路径,默认会在前面加上当前目录,组合成绝对路径. >>> os.path.abspat ...

  5. 李洪强iOS开发OC[001]-NSLog函数的使用方法

  6. win7系统如何配置JAVA环境变量

    1.在“计算机”右击选择“属性” 2.点击“高级系统设置” 3.点击“环境变量” 4.点击系统变量下面的“新建” (1)新建->变量名:JAVA_HOME变量值 C:\Program Files ...

  7. QT界面 使用QStyledItemDelegate QPainter QStyleOptionViewItem QModelIndex组合实现项的绘制

    QStyledItemDelegate类为来自模型的数据项提供了显示和编辑工具. 当在Qt项视图(例如QTableView)中显示来自模型的数据时,各个项由委托(delegate)绘制.此外,当编辑一 ...

  8. CCNA2.0笔记_Trunk&EtherChannel

    show interfaces trunk //查看Trunk信息 show interfaces fastEthernet 0/1 //查看接口二层信息 show interfaces fastEt ...

  9. 浅谈配置文件:spring-servlet.xml(spring-mvc.xml) 与 applicationContext.xml

    在搭建 spring mvc 的框架时,会有2个配置文件必不可少: spring-servlet.xml 和applicationContext.xml.第一次接触spring mvc的工程师可能会对 ...

  10. 您的位置:首页 » IOS » iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch

    原文地址:http://blog.5ibc.net/p/86562.html 前提:当时看到别人写过这个类似AssistiveTouch的demo,但是有问题,第一改变不了位置.第二切换页面后无法使用 ...